i have a problem with Builder https://filamentphp.com/docs/2.x/forms/fields#builder it don't save data in json columns.
I have a model Post:
protected $casts = [
'audio' => 'json',
'audio_title' => 'json',
];
protected $fillable = [
'audio_title',
'audio',
];
My migration
Schema::table('posts', function (Blueprint $table) {
$table->json('audio_title')->nullable();
$table->json('audio')->nullable();
});
Builder code
\Filament\Forms\Components\Builder::make('Content')
->blocks([
\Filament\Forms\Components\Builder\Block::make('Audio')
->schema([
TextInput::make('audio_title'),
FileUpload::make('audio')
->directory('audio'),
]),
]),
When i save without Builder, it work
TextInput::make('audio_title'),
FileUpload::make('audio')
>directory('audio'),
What am I doing wrong? Thank you for help!
UPD.
the filament don't encoding data in json automatically. You should to get data in method, and encode their. If you create record, use mutateFormDataBeforeCreate in Filament->resource->NameResource->Pages->CreateName.php
protected function mutateFormDataBeforeCreate(array $data): array
{
$data['content'] = json_encode($data['block']);
return $data;
}
If you edit record Filament->resource->NameResource->Pages->EditName.php
protected function mutateFormDataBeforeSave(array $data): array
{
$data['content'] = json_encode($data['block']);
return $data;
}
protected function mutateFormDataBeforeFill(array $data): array
{
$data['block'] = json_decode($data['content'], true);
return $data;
}