PHP code example of devanderson / filament-media-gallery

1. Go to this page and download the library: Download devanderson/filament-media-gallery 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/ */

    

devanderson / filament-media-gallery example snippets


use Devanderson\FilamentMediaGallery\Forms\Components\GalleryMediaField;

// In your Form schema
GalleryMediaField::make('videos_ids')
    ->mediaType('video')
    ->allowMultiple()
    ->columnSpanFull()



use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('project_image_image', function (Blueprint $table) {
            // Foreign key to your primary model's table
            $table->foreignId('project_image_id')->constrained()->onDelete('cascade');
            // Foreign key to the package's images table
            $table->foreignId('image_id')->constrained('images')->onDelete('cascade');
            // Primary key to prevent duplicates
            $table->primary(['project_image_id', 'image_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('project_image_image');
    }
};



use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('project_image_video', function (Blueprint $table) {
            $table->foreignId('project_image_id')->constrained()->onDelete('cascade');
            $table->foreignId('video_id')->constrained('videos')->onDelete('cascade');
            $table->primary(['project_image_id', 'video_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('project_image_video');
    }
};



namespace App\Models;

use Devanderson\FilamentMediaGallery\Models\Image;
use Devanderson\FilamentMediaGallery\Models\Video;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class ProjectImage extends Model
{
    protected $fillable = [
        'title',
        'description',
        // ... other fields
    ];

    /**
     * Defines the many-to-many relationship with the Image model.
     * The second argument is the name of our pivot table.
     */
    public function images(): BelongsToMany
    {
        return $this->belongsToMany(Image::class, 'project_image_image');
    }

    /**
     * Defines the many-to-many relationship with the Video model.
     * The second argument is the name of our pivot table.
     */
    public function videos(): BelongsToMany
    {
        return $this->belongsToMany(Video::class, 'project_image_video');
    }
}



namespace App\Filament\Resources\ProjectImageResource\Pages;

use App\Filament\Resources\ProjectImageResource;
use Filament\Resources\Pages\CreateRecord;
use Devanderson\FilamentMediaGallery\Traits\ProcessUploadGallery;

class CreateProjectImage extends CreateRecord
{
    use ProcessUploadGallery;

    protected static string $resource = ProjectImageResource::class;
}



namespace App\Filament\Resources\ProjectImageResource\Pages;

use App\Filament\Resources\ProjectImageResource;
use Filament\Resources\Pages\EditRecord;
use Devanderson\FilamentMediaGallery\Traits\ProcessUploadGallery;

class EditProjectImage extends EditRecord
{
    use ProcessUploadGallery;

    protected static string $resource = ProjectImageResource::class;

    /**
     * Load existing relationship IDs into form fields before filling the form
     */
    protected function mutateFormDataBeforeFill(array $data): array
    {
        // Load images IDs
        $data['images_ids'] = $this->record->images()->pluck('images.id')->toArray();
        
        // Load videos IDs
        $data['videos_ids'] = $this->record->videos()->pluck('videos.id')->toArray();

        return $data;
    }
}



namespace App\Filament\Resources\ProjectImageResource;

use Filament\Forms;
use Filament\Forms\Form;
use Devanderson\FilamentMediaGallery\Forms\Components\GalleryMediaField;

class ProjectImageForm
{
    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('title')
                    ->ultiple()
                    ->columnSpanFull(),

                // Gallery field for videos
                GalleryMediaField::make('videos_ids')
                    ->label('Gallery Videos')
                    ->mediaType('video')
                    ->allowMultiple()
                    ->columnSpanFull(),
            ]);
    }
}

GalleryMediaField::make('avatar_id')
    ->label('Profile Picture')
    ->mediaType('image')
    ->allowMultiple(false)
    ->

GalleryMediaField::make('featured_images')
    ->label('Featured Images')
    ->mediaType('image')
    ->allowMultiple()
    ->maxItems(5)

GalleryMediaField::make('videos_ids')
    ->label('Video Gallery')
    ->mediaType('video')
    ->allowMultiple()
    ->maxItems(10)

return [
    // Storage disk
    'disk' => env('MEDIA_GALLERY_DISK', 'public'),

    // Storage path
    'path' => env('MEDIA_GALLERY_PATH', 'gallery'),

    // Image settings
    'image' => [
        'allowed_extensions' => ['jpg', 'jpeg', 'png', 'gif', 'webp'],
        'max_size' => 10240, // KB
        'editor' => [
            'enabled' => true,
            'aspect_ratios' => ['16:9', '4:3', '1:1', '9:16'],
        ],
    ],

    // Video settings
    'video' => [
        'allowed_extensions' => ['mp4', 'webm', 'ogg'],
        'max_size' => 102400, // KB
        'thumbnail' => [
            'enabled' => true,
            'time' => 1.0,
            'width' => 640,
        ],
    ],

    // Gallery pagination
    'gallery' => [
        'per_page' => 24,
        'allow_multiple' => true,
        'max_items' => null,
    ],
];
bash
php artisan vendor:publish --tag="filament-media-gallery-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="filament-media-gallery-config"
bash
php artisan vendor:publish --tag="filament-media-gallery-translations"