jigsaw
jigsaw
FFilament
Created by jigsaw on 8/19/2024 in #❓┊help
How to Populate(Fill) Spatie Media Library in Custom Form?
Hello everyone. I've built a custom page with form and the form has Spatie Media Library plugin. Form components are on a polymorhpic relation. I can successfully upload an image but on the edit page, I can't retrieve the image. How can I handle this?
public function mount(int|string $record): void
{
$this->record = $this->resolveRecord($record);

$this->form->fill($this->record->content?->toArray());
}

return $form
->schema([
SpatieMediaLibraryFileUpload::make('content.featured_image')
->collection('featured_image')
->imageEditor()
->imageResizeTargetHeight('290')
->imageResizeTargetWidth('375')
->imageEditorEmptyFillColor('#ffffff')
->acceptedFileTypes(['image/*']),
])
->statePath('data');
public function mount(int|string $record): void
{
$this->record = $this->resolveRecord($record);

$this->form->fill($this->record->content?->toArray());
}

return $form
->schema([
SpatieMediaLibraryFileUpload::make('content.featured_image')
->collection('featured_image')
->imageEditor()
->imageResizeTargetHeight('290')
->imageResizeTargetWidth('375')
->imageEditorEmptyFillColor('#ffffff')
->acceptedFileTypes(['image/*']),
])
->statePath('data');
1 replies
FFilament
Created by jigsaw on 7/11/2024 in #❓┊help
Custom Page Polymorphic Model Binding
hello, I've created a custom page called "create-content". it refers to Content model which has one-to-one polymorphic relation with several models. I have a public attribute in the page called "contentable" but couldn't find a way to bind models. how can I achieve such thing?
//HospitalResource::table()
return $table
->actions([
Tables\Actions\Action::make('create-content')
->label('Create Content')
->icon('heroicon-o-plus-circle')
->url(fn(Hospital $record) => route(CreateContent::getRouteName())),
]);

//CreateContent page
class CreateContent extends Page implements HasForms
{
use InteractsWithForms;

protected static string $view = 'filament.pages.create-content';

protected static ?string $slug = 'create-content';

public ?array $data = [];

public Hospital|Unit|Treatment $contentable;

public function mount(Hospital|Unit|Treatment $contentable): void
{
$this->contentable = $contentable;

$this->form->fill();
}
//HospitalResource::table()
return $table
->actions([
Tables\Actions\Action::make('create-content')
->label('Create Content')
->icon('heroicon-o-plus-circle')
->url(fn(Hospital $record) => route(CreateContent::getRouteName())),
]);

//CreateContent page
class CreateContent extends Page implements HasForms
{
use InteractsWithForms;

protected static string $view = 'filament.pages.create-content';

protected static ?string $slug = 'create-content';

public ?array $data = [];

public Hospital|Unit|Treatment $contentable;

public function mount(Hospital|Unit|Treatment $contentable): void
{
$this->contentable = $contentable;

$this->form->fill();
}
2 replies