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.
MySQL 5.7or higher is required.
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
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 is optional, You can run this command for publishing the
php artisan vendor:publish --php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"
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', // ... ];
Here are some quick code examples:
$product = Product::find(1); $product ->addMedia($pathToFile)->toMediaCollection('images');
It can also directly handle your uploads:
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:
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
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.