1. Go to this page and download the library: Download tsrgtm/media-library library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tsrgtm / media-library example snippets
use Filament\Support\Icons\Heroicon;
use Tsrgtm\MediaLibrary\MediaLibraryPlugin;
public function panel(Panel $panel): Panel
{
return $panel
// ... other panel configuration
->plugin(
MediaLibraryPlugin::make()
->navigationGroup('Content') // Set navigation group (default: 'Content')
->navigationIcon(Heroicon::OutlinedPhoto) // Set custom menu icon
->navigationLabel('Media Manager') // Set custom menu title (default: 'Media Library')
->navigationSort(15) // Set custom menu position (default: 20)
->slug('media-manager') // Set custom URL slug (default: 'media-library')
->shouldRegisterNavigation(true) // Conditionally show in navigation menu
);
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Tsrgtm\MediaLibrary\Concerns\HasMedia;
class Post extends Model
{
use HasMedia; // That's it! No extra database columns
use App\Models\Post;
use Tsrgtm\MediaLibrary\Models\Media;
$post = Post::find(1);
$coverImage = Media::find(10);
$galleryMediaIds = [11, 12, 13];
// Attach a single media item to a specific collection (default: 'default')
$post->attachMedia($coverImage, collection: 'featured');
// Attach with custom pivot properties and explicit sort order
$post->attachMedia(
media: $coverImage,
collection: 'featured',
customProperties: ['caption' => 'Cover Photo', 'alt_text' => 'Hero Banner'],
sortOrder: 1
);
// Sync multiple media IDs to a collection (automatically handles ordering)
$post->syncMedia($galleryMediaIds, collection: 'gallery');
// Detach a single media item from a collection
$post->detachMedia($coverImage, collection: 'featured');
// Clear an entire collection
$post->clearMediaCollection('gallery');
// Retrieve all media items in a collection as an Eloquent Collection
$galleryItems = $post->getMedia('gallery');
foreach ($galleryItems as $item) {
echo $item->url; // Main asset route / public URL
echo $item->thumbnail_url; // WebP thumbnail or SVG/PNG fallback placeholder
echo $item->original_name; // Original uploaded file name
}
// Retrieve the first media item in a collection
$featuredImage = $post->getFirstMedia('featured');
if ($featuredImage) {
echo $featuredImage->url;
}
// Access the polymorphic morphToMany relationship directly
$allMedia = $post->media; // Ordered by pivot sort_order
$newCover = Media::find(25);
// Clears the 'featured' collection and attaches the new cover
$post->replaceMedia($newCover, collection: 'featured');
use Tsrgtm\MediaLibrary\Filament\Forms\Components\MediaPicker;
MediaPicker::make('featured_cover')
->label('Featured Cover Image')
->collection('featured')
->images();
use Tsrgtm\MediaLibrary\Models\Media;
use Tsrgtm\MediaLibrary\Models\MediaFolder;
Media::clearPlaceholderCache();
MediaFolder::clearThumbnailUrlCache();