Blog Detail

17

Aug
Laravel-Medialibrary - Associate files with Eloquent models cover image

arrow_back Laravel-Medialibrary - Associate files with Eloquent models

Spatie has introduced an exceptional package called Laravel Media library. This package can incorporate all kinds of files with Eloquent models. It offers a straightforward and fluent API for working. The storage of the uploaded files is managed by Laravel’s Filesystem. So You can easily store large files on another filesystem. The Pro version of the package offers Blade, Vue, and React components to manage uploads to the media library and to handle the content of a media library collection.

Requirements

  • Laravel Media Library requires PHP 7.4+ and Laravel 7+.
  • This package uses JSON columns.
  • MySQL 5.7 or higher is required.

Installation & Setup

You can install the Media library via Composer. If you want to utilize the base package, then run this command:

composer require "spatie/laravel-medialibrary:^9.0.0"

If you have a license for media library pro, you can install laravel-media-library-pro by running this command:

composer require spatie/laravel-medialibrary-pro

Preparing the database

After installation, You need to publish the migration to generate the media table:

php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="migrations"

After that, you need to run this command.

php artisan migrate

Publishing the config file

Publishing the config file is optional, You can run this command for publishing the config file:

php artisan vendor:publish --php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"

Adding a media disk

By default, the media library will store its files on Laravel’s public disk. If you want a specific disk, you should add a disk to config/filesystems.php. You’ve to add this configuration:

    ...
    'disks' => [
        ...

        'media' => [
            'driver' => 'local',
            'root'   => public_path('media'),
            'url'    => env('APP_URL').'/media',
        ],
    ...

Don’t forget to .gitignore the directory of your configured disk, So the files won’t end up in your git repo.

To store all media on that disk by default, you need to set the disk_name config value in the media-library config file to the name of the disk that you’ve added.

// config/media-library.php

return [
    'disk_name' => 'media',

    // ...
];

Code Examples!

Here are some quick code examples:

$product = Product::find(1);
$product ->addMedia($pathToFile)->toMediaCollection('images');

It can also directly handle your uploads:

$product ->addMediaFromRequest('image')->toMediaCollection('images');

If you want to store some large files on another filesystem then you’ve to follow this code:

$product->addMedia($smallFile)->toMediaCollection('downloads', 'local'); 
$product->addMedia($bigFile)->toMediaCollection('downloads', 's3');

The storage of the files is managed by Laravel’s Filesystem, so you can plug in any compatible filesystem. This package can also generate derived images such as thumbnails for images, videos, and pdfs. Once you’ve set up your model, they’re easily accessible:

$product->getMedia('images')->first()->getUrl('thumb');

If you want to dig more then, you can visit the comprehensive documentation of this package with the examples on Github.

Published at : 17-08-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project