tomatophp/filament-media-manager
The InteractsWithMediaManager trait provides convenient methods to interact with media attached to your models via both MediaManagerPicker and MediaManagerInput (Spatie Media Library).
Add the trait to your model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use TomatoPHP\FilamentMediaManager\Traits\InteractsWithMediaManager;
class Product extends Model
{
use InteractsWithMediaManager;
// ... your model code
}
Get all media attached to the model via MediaManagerPicker:
$product = Product::find(1);
$mediaItems = $product->getMediaManagerMedia();
// Get media filtered by field name (if stored in custom properties)
$avatarMedia = $product->getMediaManagerMedia('avatar');
Returns: Illuminate\Database\Eloquent\Collection of Media models
Get specific media items by their UUIDs:
$uuids = ['uuid-1', 'uuid-2', 'uuid-3'];
$mediaItems = $product->getMediaManagerMediaByUuids($uuids);
Returns: Illuminate\Database\Eloquent\Collection of Media models
Get media from a Spatie Media Library collection:
// Get all media from 'images' collection
$images = $product->getMediaManagerInputMedia('images');
// Get from default collection
$defaultMedia = $product->getMediaManagerInputMedia();
Returns: Illuminate\Database\Eloquent\Collection of Media models
Attach media to the model programmatically:
$uuids = ['uuid-1', 'uuid-2'];
$product->attachMediaManagerMedia($uuids);
Detach specific media or all media:
// Detach specific media
$product->detachMediaManagerMedia(['uuid-1', 'uuid-2']);
// Detach all media
$product->detachMediaManagerMedia();
Replace all existing media with new media (detach all, then attach new):
$newUuids = ['uuid-3', 'uuid-4', 'uuid-5'];
$product->syncMediaManagerMedia($newUuids);
Check if a specific media is attached to the model:
if ($product->hasMediaManagerMedia('uuid-1')) {
// Media is attached
}
Returns: bool
Get the first media item attached to the model:
$firstMedia = $product->getFirstMediaManagerMedia();
if ($firstMedia) {
echo $firstMedia->name;
}
Returns: Media|null
Get the URL of the first media item:
// Get original URL
$url = $product->getMediaManagerUrl();
// Get URL of a specific conversion
$thumbUrl = $product->getMediaManagerUrl('thumb');
Returns: string|null
Get URLs of all media items:
// Get all original URLs
$urls = $product->getMediaManagerUrls();
// Get all thumbnail URLs
$thumbUrls = $product->getMediaManagerUrls('thumb');
Returns: array
[@php](https://github.com/php)
$product = App\Models\Product::find(1);
$images = $product->getMediaManagerMedia();
[@endphp](https://github.com/endphp)
<div class="product-gallery">
[@foreach](https://github.com/foreach)($images as $image)
<img src="{{ $image->getUrl('thumb') }}" alt="{{ $image->name }}">
[@endforeach](https://github.com/endforeach)
</div>
[@php](https://github.com/php)
$user = auth()->user();
$avatarUrl = $user->getMediaManagerUrl('avatar-thumb') ?? '/default-avatar.png';
[@endphp](https://github.com/endphp)
<img src="{{ $avatarUrl }}" alt="User Avatar" class="avatar">
public function attachFiles(Request $request, Product $product)
{
$mediaUuids = $request->input('media_uuids');
$product->attachMediaManagerMedia($mediaUuids);
return back()->with('success', 'Files attached successfully');
}
public function update(Request $request, Product $product)
{
$product->update($request->only(['name', 'description']));
// Sync media (replace all existing with new selection)
if ($request->has('gallery_media')) {
$product->syncMediaManagerMedia($request->input('gallery_media'));
}
return back()->with('success', 'Product updated successfully');
}
[@if](https://github.com/if)($product->hasMediaManagerMedia($specificUuid))
<div class="badge">Featured Image Set</div>
[@endif](https://github.com/endif)
[@php](https://github.com/php)
$featuredImage = $product->getFirstMediaManagerMedia();
[@endphp](https://github.com/endphp)
[@if](https://github.com/if)($featuredImage)
<img src="{{ $featuredImage->getUrl() }}" alt="Featured">
[@endif](https://github.com/endif)
folder scope to ensure you get the correct mediaattachMediaManagerMedia() method uses updateOrInsert to prevent duplicatesthumb, medium, large) if configured in Spatie Media LibraryYou can store a field name in the media's custom properties to distinguish between different picker fields:
// When saving, store the field name
$media->setCustomProperty('field_name', 'avatar');
$media->save();
// Later, retrieve only avatar media
$avatarMedia = $user->getMediaManagerMedia('avatar');
This is useful when a model has multiple MediaManagerPicker fields and you need to distinguish between them programmatically.
How can I help you explore Laravel packages today?