Yes, hello. I have a question about filament. I’m implementing a search expression select box and toggle within the form method of the resource class. Here, I’m implementing it so that when the toggle button becomes true, the search expression select box becomes null. However, even when I set the toggle button to true and press the save button, the value of the search expression select box is not null, but becomes the previous value. It seems that when I toggle from false to true and back to false, the search expression select box becomes null. I’m really troubled by this. I would appreciate your help in resolving this.
public static function form(Form $form): Form
{
return $form
->schema([
Select::make('area_id')->label('adminArea')
->searchable()
->searchDebounce(1000)
->getSearchResultsUsing(fn(string $search, ?Building $record) => Area::where('area_code',
'like',
"%{$search}%")
->whereDoesntHave('Building', fn(Builder $query) => $query->when(!is_null($record),
fn(Builder $query) => $query->where('id', '<>', $record?->id)))
->limit(50)
->get()->mapWithKeys(fn(Area $area
) => [$area->id => $area->code_and_name])
)
->getOptionLabelUsing(function (?string $value): ?string {
return Area::find($value)?->code_and_name;
})->unique(table: Building::class, column: 'area_id', ignoreRecord: true)
->hidden(function (Closure $set, Closure $get) {
return $get('government_flag') === true;
}),
Toggle::make('government_flag')->label('GovernmentFlag')->lazy()
->afterStateUpdated(function (Component $component,Closure $set, Closure $get) {
if ($get('government_flag')) {
$set('area_id', null);
}
}),
]),
]);
}
I commented out hidden() to confirm if the value was becoming null. The value did become null. Additionally, I tried commenting out lazy() function. The behavior of converting to null and the behavior of hidden() have disappeared.
2
Answers
The problem has been resolved on my own.
There was a post on the official Filament Discord:
hidden() removes the select box, causing the value not to be saved. Therefore, the issue was that null values were not being reflected in the database.
I made the following fix to address this.
Toggle
BuildingResource/Pages/EditBuilding.php
Can you try removing the if condition inside
Toggle
columnafterStateUpdated
method