PHP code example of moh-slimani / media

1. Go to this page and download the library: Download moh-slimani/media library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

moh-slimani / media example snippets



    ...
    /*
     * The fully qualified class name of the media model.
     */
    'media_model' => MohSlimani\Media\Models\Media::class,
    
    ...    



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

    ...


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
    ];

    ...


/** @var File $photo */
$user->addMedia($photo)->toMediaCollection('photo');

// Or use the 


    $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),
        },
    ]



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