This is the documentation for
v9 but the latest version is
v11
.
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
Media
implements the Responsable
interface. This means that you can just return a media object to download the associated file in your browser.
use Spatie\MediaLibrary\MediaCollections\Models\Media ;
class DownloadMediaController
{
public function show (Media $mediaItem )
{
return $mediaItem ;
}
}
If you need more control you could also do the above more verbose:
use Spatie\MediaLibrary\MediaCollections\Models\Media ;
class DownloadMediaController
{
public function show (Media $mediaItem )
{
return response ()->download ($mediaItem ->getPath (), $mediaItem ->file_name );
}
}
# # Are you a visual learner?
Here's a video that shows how to download files.
VIDEO
Want to see more videos like this? Check out our free video course on how to use Laravel Media Library .