A Media Model for laravel medialibrary

0.0.16 2024-06-27 02:24 UTC

This package is auto-updated.

Last update: 2024-10-08 14:09:19 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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:

composer require moh-slimani/media

You must publish and run the migrations,this will add soft delete to the media model

php artisan vendor:publish --tag="media-migrations"
php artisan migrate

change the media_model in the media library config file

config/media-library.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

    ...
    
    /*
     * The class that contains the strategy for determining a media file's path.
     */
    'path_generator' => MohSlimani\Media\Helpers\MediaPathGenerator::class,

    ...

Usage

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

/** @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.

you can get the files like this

    $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

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.