### Install Media Library for Laravel with Composer and Artisan Source: https://github.com/roundly-consulting/media-library-for-laravel/blob/main/README.md Run these commands to install the package, publish its migrations, and run the migrations. Requires PHP 8.4, Laravel 12 or 13, ext-fileinfo, and ext-imagick or ext-gd for image variants. ```bash composer require roundly-consulting/media-library-for-laravel php artisan vendor:publish --tag="media-migrations" php artisan migrate ``` -------------------------------- ### Add and read media through the MediaLibrary facade Source: https://github.com/roundly-consulting/media-library-for-laravel/blob/main/README.md Use MediaLibrary::for($user) to get a media instance. Add a file to the 'avatar' bucket (validated, stored, and thumb rendered), then retrieve the original URL, the thumb variant URL, a blur-up placeholder data URI, and clear the media and its files. ```php use RoundlyConsulting\MediaLibrary\Facades\MediaLibrary; $avatar = MediaLibrary::for($user); $media = $avatar->add($request->file('avatar'))->toBucket('avatar'); // validated, stored, thumb rendered $avatar->url('avatar'); // the original's URL $avatar->url('avatar', 'thumb'); // the 120×120 WebP variant $media->placeholderDataUri(); // a tiny blur-up image to show while it loads $avatar->clear('avatar'); // delete the media and its files ``` -------------------------------- ### Define media buckets on an Eloquent model Source: https://github.com/roundly-consulting/media-library-for-laravel/blob/main/README.md Implement HasMedia and use InteractsWithMedia to declare buckets. The example adds an 'avatar' bucket that accepts only JPEG, PNG, and WebP images, stores a single file (new uploads replace old ones), and registers a 120x120 WebP 'thumb' variant using crop fit. ```php use Illuminate\Database\Eloquent\Model; use RoundlyConsulting\MediaLibrary\Concerns\InteractsWithMedia; use RoundlyConsulting\MediaLibrary\Contracts\HasMedia; use RoundlyConsulting\MediaLibrary\Variants\VariantRegistrar; final class User extends Model implements HasMedia { use InteractsWithMedia; public function registerMediaBuckets(): void { $this->addMediaBucket('avatar') ->singleFile() // a new upload replaces the old one ->acceptsMimeTypes(['image/jpeg', 'image/png', 'image/webp']) ->registerVariants(function (VariantRegistrar $v): void { $v->add('thumb')->fit('crop')->width(120)->height(120)->format('webp'); }); } } ```