Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Filament Media Manager Laravel Package

tomatophp/filament-media-manager

View on GitHub
Deep Wiki
Context7

Media Manager Traits

InteractsWithMediaManager

The InteractsWithMediaManager trait provides convenient methods to interact with media attached to your models via both MediaManagerPicker and MediaManagerInput (Spatie Media Library).

Installation

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
}

Available Methods

1. Get All Media (MediaManagerPicker)

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


2. Get Media by UUIDs

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


3. Get Media from Spatie Collection (MediaManagerInput)

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


4. Attach Media

Attach media to the model programmatically:

$uuids = ['uuid-1', 'uuid-2'];
$product->attachMediaManagerMedia($uuids);

5. Detach Media

Detach specific media or all media:

// Detach specific media
$product->detachMediaManagerMedia(['uuid-1', 'uuid-2']);

// Detach all media
$product->detachMediaManagerMedia();

6. Sync Media

Replace all existing media with new media (detach all, then attach new):

$newUuids = ['uuid-3', 'uuid-4', 'uuid-5'];
$product->syncMediaManagerMedia($newUuids);

7. Check if Media Exists

Check if a specific media is attached to the model:

if ($product->hasMediaManagerMedia('uuid-1')) {
    // Media is attached
}

Returns: bool


8. Get First Media

Get the first media item attached to the model:

$firstMedia = $product->getFirstMediaManagerMedia();

if ($firstMedia) {
    echo $firstMedia->name;
}

Returns: Media|null


9. Get Media URL

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


10. Get All Media URLs

Get URLs of all media items:

// Get all original URLs
$urls = $product->getMediaManagerUrls();

// Get all thumbnail URLs
$thumbUrls = $product->getMediaManagerUrls('thumb');

Returns: array


Usage Examples

Example 1: Display Product Images

[@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>

Example 2: Display User Avatar

[@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">

Example 3: Attach Media in Controller

public function attachFiles(Request $request, Product $product)
{
    $mediaUuids = $request->input('media_uuids');
    $product->attachMediaManagerMedia($mediaUuids);

    return back()->with('success', 'Files attached successfully');
}

Example 4: Sync Media on Update

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');
}

Example 5: Check and Display

[@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)

Notes

  • All methods that query media bypass the global folder scope to ensure you get the correct media
  • The attachMediaManagerMedia() method uses updateOrInsert to prevent duplicates
  • Media URLs can be generated with conversions (e.g., thumb, medium, large) if configured in Spatie Media Library
  • The trait works with both MediaManagerPicker (form component) and MediaManagerInput (Spatie collections)

Advanced: Custom Field Names

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

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope