PHP code example of tsrgtm / media-library

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();

MediaPicker::make('product_gallery')
    ->label('Product Gallery')
    ->collection('gallery')
    ->multiple()
    ->minItems(1)
    ->maxItems(8)
    ->reorderable()
    ->removable();

// Filter by media kinds ('image', 'video', 'audio', 'document', 'archive')
MediaPicker::make('downloadable_file')
    ->collection('downloads')
    ->acceptedKinds(['document', 'archive']);

// Filter by explicit file extensions
MediaPicker::make('pdf_document')
    ->collection('documents')
    ->acceptedExtensions(['pdf', 'docx', 'xlsx']);

// Filter by MIME types
MediaPicker::make('audio_track')
    ->collection('audio')
    ->acceptedMimeTypes(['audio/mpeg', 'audio/wav', 'audio/ogg']);

MediaPicker::make('brand_logo')
    ->collection('logo')
    ->defaultFolder('brand-assets')
    ->customProperties([
        'usage' => 'header-logo',
    ]);

use Tsrgtm\MediaLibrary\Models\Media;
use Tsrgtm\MediaLibrary\Models\MediaFolder;

Media::clearPlaceholderCache();
MediaFolder::clearThumbnailUrlCache();

return [
    // Route Prefix & Authentication Middleware
    'route_prefix' => env('MEDIA_LIBRARY_ROUTE_PREFIX', 'media'),
    'middleware'   => ['web', 'auth'],

    // Disks
    'disk'           => env('MEDIA_DISK', 'public'),
    'temporary_disk' => env('MEDIA_TEMP_DISK', 'local'),

    // Upload Constraints
    'small_file_threshold'        => 10 * 1024 * 1024, // 10MB
    'chunk_size'                  => 8 * 1024 * 1024,  // 8MB
    'maximum_upload_size'         => 5 * 1024 * 1024 * 1024, // 5GB
    'concurrent_uploads'          => 4,

    // Image WebP Conversions & Quality
    'image' => [
        'quality' => 82,
        'conversions' => [
            'thumbnail' => 320,
            'small'     => 640,
            'medium'    => 1024,
            'large'     => 1600,
        ],
    ],

    // Queue Engine
    'queue_connection' => env('MEDIA_QUEUE_CONNECTION', 'database'),
];
bash
php artisan media-library:install
bash
php artisan migrate
bash
php artisan media-library:install
bash
php artisan media-library:update
bash
php artisan media-library:uninstall
bash
php artisan queue:work
bash
php vendor/bin/pest