$npx -y skills add gdarko/laravel-vue-starter --skill medialibrary-developmentBuild 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.
| 1 | # Media Library Development |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Use spatie/laravel-medialibrary to associate files with Eloquent models. Supports image/video conversions, responsive images, multiple collections, and various storage disks. |
| 6 | |
| 7 | ## When to Activate |
| 8 | |
| 9 | - Activate when working with file uploads, media attachments, or image processing in Laravel. |
| 10 | - Activate when code references `HasMedia`, `InteractsWithMedia`, the `Media` model, or media collections/conversions. |
| 11 | - Activate when the user wants to add, retrieve, convert, or manage files attached to Eloquent models. |
| 12 | |
| 13 | ## Scope |
| 14 | |
| 15 | - In scope: media uploads, collections, conversions, responsive images, custom properties, file retrieval, path/URL generation. |
| 16 | - Out of scope: general file storage without Eloquent association, non-Laravel frameworks. |
| 17 | |
| 18 | ## Workflow |
| 19 | |
| 20 | 1. Identify the task (model setup, adding media, defining conversions, retrieving files, etc.). |
| 21 | 2. Read `references/medialibrary-guide.md` and focus on the relevant section. |
| 22 | 3. Apply the patterns from the reference, keeping code minimal and Laravel-native. |
| 23 | |
| 24 | ## Core Concepts |
| 25 | |
| 26 | ### Model Setup |
| 27 | |
| 28 | Every model that should have media must implement `HasMedia` and use the `InteractsWithMedia` trait: |
| 29 | |
| 30 | ```php |
| 31 | use Spatie\MediaLibrary\HasMedia; |
| 32 | use Spatie\MediaLibrary\InteractsWithMedia; |
| 33 | |
| 34 | class BlogPost extends Model implements HasMedia |
| 35 | { |
| 36 | use InteractsWithMedia; |
| 37 | } |
| 38 | ``` |
| 39 | |
| 40 | ### Adding Media |
| 41 | |
| 42 | ```php |
| 43 | $blogPost->addMedia($file)->toMediaCollection('images'); |
| 44 | $blogPost->addMediaFromUrl($url)->toMediaCollection('images'); |
| 45 | $blogPost->addMediaFromRequest('file')->toMediaCollection('images'); |
| 46 | ``` |
| 47 | |
| 48 | ### Defining Collections |
| 49 | |
| 50 | ```php |
| 51 | public function registerMediaCollections(): void |
| 52 | { |
| 53 | $this->addMediaCollection('avatar')->singleFile(); |
| 54 | $this->addMediaCollection('downloads')->useDisk('s3'); |
| 55 | } |
| 56 | ``` |
| 57 | |
| 58 | ### Defining Conversions |
| 59 | |
| 60 | ```php |
| 61 | use Spatie\MediaLibrary\MediaCollections\Models\Media; |
| 62 | use Spatie\Image\Enums\Fit; |
| 63 | |
| 64 | public function registerMediaConversions(?Media $media = null): void |
| 65 | { |
| 66 | $this->addMediaConversion('thumb') |
| 67 | ->fit(Fit::Contain, 300, 300) |
| 68 | ->nonQueued(); |
| 69 | } |
| 70 | ``` |
| 71 | |
| 72 | ### Retrieving Media |
| 73 | |
| 74 | ```php |
| 75 | $url = $model->getFirstMediaUrl('images'); |
| 76 | $thumbUrl = $model->getFirstMediaUrl('images', 'thumb'); |
| 77 | $allMedia = $model->getMedia('images'); |
| 78 | ``` |
| 79 | |
| 80 | ## Do and Don't |
| 81 | |
| 82 | Do: |
| 83 | - Always implement the `HasMedia` interface alongside the `InteractsWithMedia` trait. |
| 84 | - Use `?Media $media = null` as the parameter for `registerMediaConversions()`. |
| 85 | - Call `->toMediaCollection()` to finalize adding media. |
| 86 | - Use `->nonQueued()` for conversions that should run synchronously. |
| 87 | - Use `->singleFile()` on collections that should only hold one file. |
| 88 | - Use `Spatie\Image\Enums\Fit` enum values for fit methods. |
| 89 | |
| 90 | Don't: |
| 91 | - Don't forget to run `php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-migrations"` before migrating. |
| 92 | - Don't use `env()` for disk configuration; use `config()` or set it in `config/media-library.php`. |
| 93 | - Don't call `addMedia()` without calling `toMediaCollection()` — the media won't be saved. |
| 94 | - Don't reference conversion names that aren't registered in `registerMediaConversions()`. |
| 95 | |
| 96 | ## References |
| 97 | |
| 98 | - `references/medialibrary-guide.md` |