This is the documentation for
v3 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
If you have different types of files that you want to associate,
you can put them in their own collection.
$newsItem = News::find(1);
$newsItem->addMedia($pathToImage)->toCollection('images');
$newsItem->addMedia($pathToAnotherImage)->toCollection('images');
$newsItem->addMedia($pathToPdfFile)->toCollection('downloads');
$newsItem->addMedia($pathToAnExcelFile)->toCollection('downloads');
All media in a specific collection can be retrieved like this:
$newsItem->getMedia('images');
// Returns media objects for all files in the images collection
$newsItem->getMedia('downloads');
// Returns media objects for all files in the downloads collection
A collection can have any name you want. If you don't specify a name, the file will be added to a default
-collection.
You can clear out a specific collection by passing the name to clearMediaCollection
:
$newsItem->clearMediaCollection('images');