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;
};
}'
Solution:Jump to solution
I have succeeded, currently I am using code like this
->hidden(fn ($livewire) => (SheetLeave::where('user_id', $livewire->data['user_id'])->first()?->available_leaves ?? 0) === 0)
but will this cause any problems...19 Replies
Please format your code properly, so it's easier to read like mentioned in #✅┊rules
This is my code

Not sure whether you can use
$get
from an action. Can you try $livewire
? Then you could access the data via $livewire->data
thanks for the suggestion, The error has been resolved successfully, but the trait condition is not running, is the usage method wrong?
The condition of the field does not appear if the trait returns a value from the assessor of 0, but if it returns a value of more than 0, the field appears.


The condition of the field does not appear if the trait returns a value from the assessor of 0, but if it returns a value of more than 0, the field appears.I don't know what you mean. Isn't that what you want?
true, but I want the create action code to have a visible condition that is the opposite of this code, if this code appears then the create action disappears.
for now I feel that the create action does not run the logic in my trait code


for now I feel that the create action does not run the logic in my trait codeThat should be easy to debug. Check whether your code runs and whether the
user_id
is correct.if in trait i use $get and in action i use $livewire can be conflict? because when i debug,data was correctly but the action button not work
Yes, if you don't change the implementation at all, that will be a conflict. Did you debug the actual action?
i was implements in action
protected function getFormActions(): array
{
return [
Action::make('create')
->label(__('Request'))
->submit('create')
->hidden(fn ($livewire) => HasAvailableLeaves::hideIfNoAvailableLeaves($livewire))
];
}
but the condition not depends on trait
in trait still this
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;
};
}
I said you need to use
$livewire->data
. You can't just pass Livewire and expect it to work the same
->hidden(fn ($livewire) => HasAvailableLeaves::hideIfNoAvailableLeaves($livewire))This probably won't do anything, because you are returning another Closure in your Closure
I know what you mean like this right
->hidden(fn ($livewire) => HasAvailableLeaves::hideIfNoAvailableLeaves($livewire->data))
I've tried it before but the result is the same, the button disappears in all conditions and when debugged the result doesn't change$data
is still not the same like$get()
. The first one is an array, the second one is a method.
What are you actually doing to debug this with the action?
I would expect this to throw an error in the action.
Is the "Request" method rendered at all, when you don't use ->hidden()
?sorry, I'm just learning to use filament,I don't understand what you mean,
i do debug like this
->hidden(fn ($livewire) => dd($livewire->data))
and the resulting data is appropriate
but the action request not show when the data is not '0'Okay, but that means that you aren't debugging the code inside
HasAvailableLeaves::hide...
So to debug the actual code:
1. Check whether HasAvailableLeaves::
is executed for the action
2. Check what $livewire->data
is.
3. Check what $userId
is
4. Check what $availableLeaves
is
You probably need to refactor this code, because $livewire->data
is not a function you can call, and I'm confused why you aren't getting an error for that.Solution
I have succeeded, currently I am using code like this
->hidden(fn ($livewire) => (SheetLeave::where('user_id', $livewire->data['user_id'])->first()?->available_leaves ?? 0) === 0)
but will this cause any problemsIs
available_leaves
a property on the model? Then you could use this instead
Otherwise this looks good to me.no,this is assessor on the model,and this is for sum all total_days if user_id same
Then keep it that way