NewWe open-sourced 50+ Laravel packages
Custom AI apps, agents and automation — Roundly ConsultingRoundly
All packages

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 media

The 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 bucket

Media 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 id

Show 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 crypto

By 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.