https://github.com/slimani-dev/media
A model for Laravel Medialibrary
https://github.com/slimani-dev/media
Last synced: about 1 year ago
JSON representation
A model for Laravel Medialibrary
- Host: GitHub
- URL: https://github.com/slimani-dev/media
- Owner: slimani-dev
- License: mit
- Created: 2024-02-19T04:58:44.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-02-03T14:00:25.000Z (over 1 year ago)
- Last Synced: 2025-02-24T07:07:31.334Z (over 1 year ago)
- Language: PHP
- Size: 63.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# A Media Model for laravel medialibrary
[](https://packagist.org/packages/moh-slimani/media)
[](https://github.com/moh-slimani/media/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/moh-slimani/media/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[](https://packagist.org/packages/moh-slimani/media)
This package simplifies the integration of Spatie MediaLibrary by offering a streamlined approach to registering
and managing media assets within Laravel applications, effortlessly cast media to a simple format, leveraging a
path generator that enhances readability and organization. Seamlessly handle media registration
while maintaining all functionalities of The MediaLibrary package.
## Installation
You can install the package via composer:
```bash
composer require moh-slimani/media
```
You must publish and run the migrations,this will add soft delete to the media model
```bash
php artisan vendor:publish --tag="media-migrations"
php artisan migrate
```
change the media_model in the media library config file
`config/media-library.php`
```php
...
/*
* The fully qualified class name of the media model.
*/
'media_model' => MohSlimani\Media\Models\Media::class,
...
```
**Optional :** you can change the path generator in the media library config file
```php
...
/*
* The class that contains the strategy for determining a media file's path.
*/
'path_generator' => MohSlimani\Media\Helpers\MediaPathGenerator::class,
...
```
## Usage
```php
use MohSlimani\Media\Traits\UseMediaModel
use MohSlimani\Media\Media
use Spatie\MediaLibrary\HasMedia;
class User extends Authenticatable implements HasMedia
{
// you don't need to user InteractsWithMedia
use HasApiTokens, HasFactory, Notifiable, UseMediaModel;
/**
* This array should contain the list of media keys to be registered.
*
* @var array $files
* @example ['photo' => Media::SINGLE_FILE, 'files' => Media::MULTIPLE_FILES]
*/
protected array $files = [
'photo' => Media::SINGLE_FILE,
'cv', // Media::SINGLE_FILE is the default
'files' => Media::MULTIPLE_FILES
];
...
```
After that you can add files it like you used to using the `medialibrary` package
```php
/** @var File $photo */
$user->addMedia($photo)->toMediaCollection('photo');
// Or use the included function
$user->addMediaFiles($photo, 'photo');
```
> the function addMediaFiles to simplify the process of adding media files to collections. This function allows you to
> add a single file to a specified collection and provides an option to keep or delete existing files in the collection.
> it will also generate a unique code based on current time to prepend to the file name
> You can find more information about this and other changes in the [CHANGELOG](CHANGELOG.md).
you can get the files like this
```php
$user->photo
[
"id" => 15,
"name" => "IMG_7833",
"url" => "http://media.test/storage/Users/1/photo/IMG_7833.jpg",
"size" => 249686,
"mime" => "image/jpeg",
"type" => "image",
"created_at" => Illuminate\Support\Carbon @1705898712 {#6527
date: 2024-02-19 00:00:00.0 UTC (+00:00),
},
"updated_at" => Illuminate\Support\Carbon @1705898712 {#6782
date: 2024-02-19 00:00:00.0 UTC (+00:00),
},
]
```
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Credits
- [Mohamed slimani](https://github.com/moh-slimani)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.