Media Library Development
Overview
Use spatie/laravel-medialibrary to associate files with Eloquent models. Supports image/video conversions, responsive images, multiple collections, and various storage disks.
When to Activate
- Activate when working with file uploads, media attachments, or image processing in Laravel.
- Activate when code references
HasMedia, InteractsWithMedia, the Media model, or media collections/conversions.
- Activate when the user wants to add, retrieve, convert, or manage files attached to Eloquent models.
Scope
- In scope: media uploads, collections, conversions, responsive images, custom properties, file retrieval, path/URL generation.
- Out of scope: general file storage without Eloquent association, non-Laravel frameworks.
Workflow
- Identify the task (model setup, adding media, defining conversions, retrieving files, etc.).
- Read
references/medialibrary-guide.md and focus on the relevant section.
- Apply the patterns from the reference, keeping code minimal and Laravel-native.
Core Concepts
Model Setup
Every model that should have media must implement HasMedia and use the InteractsWithMedia trait:
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class BlogPost extends Model implements HasMedia
{
use InteractsWithMedia;
}
Adding Media
$blogPost->addMedia($file)->toMediaCollection('images');
$blogPost->addMediaFromUrl($url)->toMediaCollection('images');
$blogPost->addMediaFromRequest('file')->toMediaCollection('images');
Defining Collections
public function registerMediaCollections(): void
{
$this->addMediaCollection('avatar')->singleFile();
$this->addMediaCollection('downloads')->useDisk('s3');
}
Defining Conversions
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\Image\Enums\Fit;
public function registerMediaConversions(?Media $media = null): void
{
$this->addMediaConversion('thumb')
->fit(Fit::Contain, 300, 300)
->nonQueued();
}
Retrieving Media
$url = $model->getFirstMediaUrl('images');
$thumbUrl = $model->getFirstMediaUrl('images', 'thumb');
$allMedia = $model->getMedia('images');
Do and Don't
Do:
- Always implement the
HasMedia interface alongside the InteractsWithMedia trait.
- Use
?Media $media = null as the parameter for registerMediaConversions().
- Call
->toMediaCollection() to finalize adding media.
- Use
->nonQueued() for conversions that should run synchronously.
- Use
->singleFile() on collections that should only hold one file.
- Use
Spatie\Image\Enums\Fit enum values for fit methods.
Don't:
- Don't forget to run
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-migrations" before migrating.
- Don't use
env() for disk configuration; use config() or set it in config/media-library.php.
- Don't call
addMedia() without calling toMediaCollection() — the media won't be saved.
- Don't reference conversion names that aren't registered in
registerMediaConversions().
References
references/medialibrary-guide.md
1---2name: medialibrary-development3description: Build and work with spatie/laravel-medialibrary features including associating files with Eloquent models, defining media collections and conversions, generating responsive images, and retrieving media URLs and paths.4license: MIT5---67# Media Library Development89## Overview1011Use spatie/laravel-medialibrary to associate files with Eloquent models. Supports image/video conversions, responsive images, multiple collections, and various storage disks.1213## When to Activate1415- Activate when working with file uploads, media attachments, or image processing in Laravel.16- Activate when code references `HasMedia`, `InteractsWithMedia`, the `Media` model, or media collections/conversions.17- Activate when the user wants to add, retrieve, convert, or manage files attached to Eloquent models.1819## Scope2021- In scope: media uploads, collections, conversions, responsive images, custom properties, file retrieval, path/URL generation.22- Out of scope: general file storage without Eloquent association, non-Laravel frameworks.2324## Workflow25261. Identify the task (model setup, adding media, defining conversions, retrieving files, etc.).272. Read `references/medialibrary-guide.md` and focus on the relevant section.283. Apply the patterns from the reference, keeping code minimal and Laravel-native.2930## Core Concepts3132### Model Setup3334Every model that should have media must implement `HasMedia` and use the `InteractsWithMedia` trait:3536```php37use Spatie\MediaLibrary\HasMedia;38use Spatie\MediaLibrary\InteractsWithMedia;3940class BlogPost extends Model implements HasMedia41{42 use InteractsWithMedia;43}44```4546### Adding Media4748```php49$blogPost->addMedia($file)->toMediaCollection('images');50$blogPost->addMediaFromUrl($url)->toMediaCollection('images');51$blogPost->addMediaFromRequest('file')->toMediaCollection('images');52```5354### Defining Collections5556```php57public function registerMediaCollections(): void58{59 $this->addMediaCollection('avatar')->singleFile();60 $this->addMediaCollection('downloads')->useDisk('s3');61}62```6364### Defining Conversions6566```php67use Spatie\MediaLibrary\MediaCollections\Models\Media;68use Spatie\Image\Enums\Fit;6970public function registerMediaConversions(?Media $media = null): void71{72 $this->addMediaConversion('thumb')73 ->fit(Fit::Contain, 300, 300)74 ->nonQueued();75}76```7778### Retrieving Media7980```php81$url = $model->getFirstMediaUrl('images');82$thumbUrl = $model->getFirstMediaUrl('images', 'thumb');83$allMedia = $model->getMedia('images');84```8586## Do and Don't8788Do:89- Always implement the `HasMedia` interface alongside the `InteractsWithMedia` trait.90- Use `?Media $media = null` as the parameter for `registerMediaConversions()`.91- Call `->toMediaCollection()` to finalize adding media.92- Use `->nonQueued()` for conversions that should run synchronously.93- Use `->singleFile()` on collections that should only hold one file.94- Use `Spatie\Image\Enums\Fit` enum values for fit methods.9596Don't:97- Don't forget to run `php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-migrations"` before migrating.98- Don't use `env()` for disk configuration; use `config()` or set it in `config/media-library.php`.99- Don't call `addMedia()` without calling `toMediaCollection()` — the media won't be saved.100- Don't reference conversion names that aren't registered in `registerMediaConversions()`.101102## References103104- `references/medialibrary-guide.md`