feat: Implement Spatie Media Library for Feed model with custom path generation and Filament integration.

This commit is contained in:
Yoga Pangestu 2026-02-13 15:46:27 +07:00
parent bd86ff2159
commit 20b76b6b04
6 changed files with 816 additions and 235 deletions

View File

@ -14,6 +14,7 @@
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
@ -81,6 +82,19 @@ public static function form(Schema $schema): Schema
->prefix('Rp')
->helperText('Harga per satuan yang dipilih.')
->dehydrateStateUsing(fn ($state) => str_replace(',', '', $state)),
SpatieMediaLibraryFileUpload::make('image')
->label('Gambar')
->disk(config('filesystems.default'))
->acceptedFileTypes(['image/*'])
->maxSize(1024 * 3)
->collection('feed')
->customProperties(fn (): array => [
'feature' => 'feeds',
'date' => now()->toDateString(),
])
->image()
->required(),
])
->columns(1);
}

View File

@ -0,0 +1,40 @@
<?php
namespace App\MediaLibrary;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Support\PathGenerator\PathGenerator;
class CustomPathGenerator implements PathGenerator
{
public function getPath(Media $media): string
{
$feature = $media->getCustomProperty('feature', 'misc');
$date = $media->getCustomProperty('date', now()->toDateString());
$docType = $media->getCustomProperty('doc_type');
$feature = str($feature)->slug();
$date = str($date);
$docType = $docType ? str($docType)->slug() : null;
$path = "{$feature}/";
if ($docType) {
$path .= "{$docType}/";
}
$path .= "{$date}/";
return $path;
}
public function getPathForConversions(Media $media): string
{
return $this->getPath($media).'conversions/';
}
public function getPathForResponsiveImages(Media $media): string
{
return $this->getPath($media).'responsive/';
}
}

View File

@ -7,10 +7,12 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Feed extends Model
class Feed extends Model implements HasMedia
{
use SoftDeletes;
use InteractsWithMedia, SoftDeletes;
protected $guarded = ['id'];

View File

@ -10,6 +10,7 @@
"bezhansalleh/filament-shield": "^4.1",
"diogogpinto/filament-auth-ui-enhancer": "^2.0",
"filament/filament": "^4.0",
"filament/spatie-laravel-media-library-plugin": "^4.0",
"laravel/framework": "^12.0",
"laravel/tinker": "^2.10.1"
},

958
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,32 @@
<?php
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('media', function (Blueprint $table) {
$table->id();
$table->morphs('model');
$table->uuid()->nullable()->unique();
$table->string('collection_name');
$table->string('name');
$table->string('file_name');
$table->string('mime_type')->nullable();
$table->string('disk');
$table->string('conversions_disk')->nullable();
$table->unsignedBigInteger('size');
$table->json('manipulations');
$table->json('custom_properties');
$table->json('generated_conversions');
$table->json('responsive_images');
$table->unsignedInteger('order_column')->nullable()->index();
$table->nullableTimestamps();
});
}
};