F
Filament2mo ago
Mike.

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:
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...
Jump to solution
19 Replies
Dennis Koch
Dennis Koch2mo ago
Please format your code properly, so it's easier to read like mentioned in #✅┊rules
Mike.
Mike.OP2mo ago
This is my code
No description
Dennis Koch
Dennis Koch2mo ago
Not sure whether you can use $get from an action. Can you try $livewire? Then you could access the data via $livewire->data
Mike.
Mike.OP2mo ago
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.
No description
No description
Dennis Koch
Dennis Koch2mo ago
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?
Mike.
Mike.OP2mo ago
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
No description
No description
Dennis Koch
Dennis Koch2mo ago
for now I feel that the create action does not run the logic in my trait code
That should be easy to debug. Check whether your code runs and whether the user_id is correct.
Mike.
Mike.OP2mo ago
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
Dennis Koch
Dennis Koch2mo ago
Yes, if you don't change the implementation at all, that will be a conflict. Did you debug the actual action?
Mike.
Mike.OP2mo ago
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; }; }
Dennis Koch
Dennis Koch2mo ago
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
Mike.
Mike.OP2mo ago
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
Dennis Koch
Dennis Koch2mo ago
$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()?
Mike.
Mike.OP2mo ago
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'
Dennis Koch
Dennis Koch2mo ago
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
Mike.
Mike.2mo ago
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
Dennis Koch
Dennis Koch2mo ago
Is available_leaves a property on the model? Then you could use this instead
SheetLeave::where('user_id', $livewire->data['user_id'])
->limit(1)
->value('available_leaves') ?? 0) === 0
SheetLeave::where('user_id', $livewire->data['user_id'])
->limit(1)
->value('available_leaves') ?? 0) === 0
Otherwise this looks good to me.
Mike.
Mike.OP2mo ago
no,this is assessor on the model,and this is for sum all total_days if user_id same
Dennis Koch
Dennis Koch2mo ago
Then keep it that way

Did you find this page helpful?