This is the documentation for
v4 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
You can associate a file with a model like this:
$newsItem = News::find(1);
$newsItem->addMedia($pathToFile)
->toMediaLibrary();
The file will now be associated with the newsitem and will be moved to the disk you've configured.
If you want to preserve the original file, you can call preservingOriginal
:
$newsItem->addMedia($pathToFile)
->preservingOriginal()
->toMediaLibrary();
v3.8+ You can also add a remote file to the media library:
$url = 'http://medialibrary.spatie.be/assets/images/mountain.jpg';
$newsItem->addMediaFromUrl($url)
->toMediaLibrary();