Hide Create an action in the form of a filament
I have a code like this
'use HasAvailableLeaves;
protected function getFormActions(): array
{
return [
CreateAction::make('create')
->label(__('Request'))
->submit('create')
->hidden(HasAvailableLeaves::hideIfNoAvailableLeaves())
];
}'
I want this code to disappear according to the trait that I have created, but in a code condition like this I get an error
An attempt was made to evaluate a closure for [Filament\Actions\CreateAction], but [$get] was unresolvable.
Code in trait
'public static function hideIfNoAvailableLeaves(): Closure
{
return function ($get) {
$userId = $get('user_id');
if (!$userId) {
return false;
}
$availableLeaves = SheetLeave::where('user_id', $userId)->first()?->available_leaves ?? 0;
return $availableLeaves === 0;
};
}'
'use HasAvailableLeaves;
protected function getFormActions(): array
{
return [
CreateAction::make('create')
->label(__('Request'))
->submit('create')
->hidden(HasAvailableLeaves::hideIfNoAvailableLeaves())
];
}'
I want this code to disappear according to the trait that I have created, but in a code condition like this I get an error
An attempt was made to evaluate a closure for [Filament\Actions\CreateAction], but [$get] was unresolvable.
Code in trait
'public static function hideIfNoAvailableLeaves(): Closure
{
return function ($get) {
$userId = $get('user_id');
if (!$userId) {
return false;
}
$availableLeaves = SheetLeave::where('user_id', $userId)->first()?->available_leaves ?? 0;
return $availableLeaves === 0;
};
}'
Solution
I have succeeded, currently I am using code like this
but will this cause any problems
->hidden(fn ($livewire) => (SheetLeave::where('user_id', $livewire->data['user_id'])->first()?->available_leaves ?? 0) === 0)but will this cause any problems