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(),
]);
}
}