Conditionally add and update relations
Currently I got this situation.
A user can have only 1 shipping address and only 1 billing address.
In Filament's admin panel's user form, admin can change user's info.
However I want to set restrictions, so that admin cant give a user >1 address of type shipping, and cant give a user >1 address of type billing.
That also implies that when the user already got 1 billing address, the admin can't change that user's shipping address to type=billing.
How can something like that be done?
Solution:Jump to solution
Create a policy according to the laravel docs then handle the conditions in each method based on your apps authorization. Filament defaults to Laravel’s policies to handle model authorization. https://laravel.com/docs/11.x/authorization#creating-policies
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
13 Replies
In most cases you can pass Closures or Callables to filament methods.
Yes. But a more detailed answer please
Please provide some code you are using
Here:
(Updated. Sorry about the screenshot earlier. I replaced that image attachment with text code now)
Please read #✅┊rules on how to provide code.
Sorry I am out and I only have my phone
This would normally be handled by a laravel policy. Filament is still just laravel under the hood.
How exactly do you do that?
Something more specific please
Solution
Create a policy according to the laravel docs then handle the conditions in each method based on your apps authorization. Filament defaults to Laravel’s policies to handle model authorization. https://laravel.com/docs/11.x/authorization#creating-policies
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Thank you
Actually, policy is only half the answer.
Normally, you create a policy, then you apply it in controller method, like
$this->authorize('action', $modelInstance);
But Filament have its own controllers. It doesnt go thru any of the controllers that I wrote.
Hence in this situation, how can policy (or any type of pre-saving checks) be done upon the click of the add/delete/edit buttons?You can add ->authorize() in your action component. In this case, you are using Repeater you can customize addAction by follow with https://filamentphp.com/docs/3.x/forms/fields/repeater
->addAction(fn($action)=> $action->authorize(...