All packages
Media Library for Laravel
Reading media
The for($model) handle reads, clears and deletes one owner's media. It never returns another owner's rows:
$media = MediaLibrary::for($user);
$media->get('avatar'); // Collection<Media>, ordered
$media->first('avatar'); // ?Media
$media->has('avatar'); // bool
$media->find($uuid); // ?Media — only if this user owns it
$media->url('avatar'); // first URL, the bucket fallback, or ''
$media->url('avatar', 'thumb'); // a variant's URL
$media->temporaryUrl('avatar', expiry: now()->addMinutes(10));
$media->clear('avatar'); // delete every media in the bucket; returns the count
$media->delete($photo); // throws MediaDoesNotBelongToModel for someone else's mediaThe trait shorthand
The trait mirrors the readers on the model:
$user->getMedia('avatar'); // Collection<Media>, ordered
$user->getFirstMedia('avatar'); // ?Media
$user->getFirstMediaUrl('avatar', 'thumb'); // a variant's URL, the fallback, or ''
$user->getFirstTemporaryUrl('avatar'); // time-limited URL for private media
$user->hasMedia('avatar'); // bool
$user->clearMediaBucket('avatar'); // delete every media in the bucketMedia attributes
Each Media exposes the usual file metadata plus arbitrary custom properties:
$media->name; // display name
$media->file_name; // stored filename
$media->mime_type; // 'image/webp'
$media->size; // bytes
$media->custom_properties; // array
$media->getCustomProperty('alt');
$media->uuid; // stable public idShow your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.