@@ -137,6 +137,38 @@ MEDIA_MAXKB_DEFAULT=10240
137137
1381381 ) Ensure your target Eloquent model implements ` Spatie\MediaLibrary\HasMedia ` and is ** saved** .
139139
140+ #### Model Setup (Spatie Media Library)
141+
142+ Your model must implement ` HasMedia ` and be ** saved** before attaching media.
143+
144+ ``` php
145+ use Spatie\MediaLibrary\HasMedia;
146+ use Spatie\MediaLibrary\InteractsWithMedia;
147+ use Spatie\MediaLibrary\MediaCollections\Models\Media;
148+
149+ class Post extends Model implements HasMedia
150+ {
151+ use InteractsWithMedia;
152+
153+ public function registerMediaCollections(): void
154+ {
155+ $this->addMediaCollection('photos') // matches collection="photos"
156+ ->useDisk('public') // or 's3'
157+ ->withResponsiveImages(); // optional
158+
159+ // If you also have avatars somewhere:
160+ $this->addMediaCollection('avatars')->singleFile();
161+ }
162+
163+ public function registerMediaConversions(Media $media = null): void
164+ {
165+ $this->addMediaConversion('thumb')
166+ ->fit('contain', 256, 256)
167+ ->performOnCollections('photos', 'avatars') // scope to specific collections
168+ ->nonQueued();
169+ }
170+ }
171+ ```
1401722) Include Livewire & Alpine (usually in your app layout):
141173
142174```html
@@ -305,34 +337,6 @@ Example:
305337
306338---
307339
308- ## Model Setup (Spatie Media Library)
309-
310- Your model must implement ` HasMedia ` and be ** saved** before attaching media.
311-
312- ``` php
313- use Spatie\MediaLibrary\HasMedia;
314- use Spatie\MediaLibrary\InteractsWithMedia;
315-
316- class User extends Model implements HasMedia
317- {
318- use InteractsWithMedia;
319-
320- public function registerMediaCollections(): void
321- {
322- $this->addMediaCollection('images');
323- $this->addMediaCollection('avatars');
324- }
325-
326- // Optional thumbnail conversion for the list
327- public function registerMediaConversions(\Spatie\MediaLibrary\MediaCollections\Models\Media $media = null): void
328- {
329- $this->addMediaConversion('thumb')
330- ->fit('contain', 256, 256)
331- ->nonQueued();
332- }
333- }
334- ```
335-
336340> The list view tries ` getUrl('thumb') ` and falls back to ` getUrl() ` if no conversion is available.
337341
338342---
0 commit comments