This is the documentation for
v6 but the latest version is
v10
.
You can switch versions in the menu on the left/at the top.
Check your current version with the following command:
composer show spatie/laravel-medialibrary
Medialibrary is a Laravel (5.5 and up) package that can associate all sorts of files with Eloquent models. It provides a simple, fluent API to work with.
Are you a visual learner? Then watch this video that demonstrates what the package can do.
{{< youtube 4lq_6JLR2H8 >}}
Here are some quick code examples:
$newsItem = News::find(1);
$newsItem->addMedia($pathToFile)->toMediaCollection('images');
It can also directly handle your uploads:
$newsItem->addMediaFromRequest('image')->toMediaCollection('images');
Want to store some large files on another filesystem? No problem:
$newsItem->addMedia($smallFile)->toMediaCollection('downloads', 'local');
$newsItem->addMedia($bigFile)->toMediaCollection('downloads', 's3');
The storage of the files is handled by Laravel's Filesystem, so you can plug in any compatible filesystem.
The package can also generate derived images such as thumbnails for images, video's and pdf's. Once you've set up your model, they're easily accessible:
$newsItem->getMedia('images')->first()->getUrl('thumb');
##We have badges!