Modify form or table in extended resource
I have admin and app panel, I share resource between these and use extend in my app panel.
When you extend a resource you can modify "label", or "icon" or "getNavigationGroup" but not "form" or "table".
Is it possible ?
2 Replies
I don't
extend
resources. I make new resources, and either call the static methods from the other resource, or stuff them all into a trait that is used by multiple resources.
Since they're static
it's easy to tell my extra resource form() method to return MainResource::form();
but define my own custom table() method.
But you can't change a table that's returned from another resource. So, that's where a trait comes in handy: define all your "possible" fields as custom methods in the trait (they just return the field/column definition), and then build your forms and tables using calls to those static custom methods. Then each resource can call whichever ones it wants, excluding irrelevant ones or rearranging them, etc.Sometimes a code sample helps. Here's an example from the Filament Demo app: https://github.com/filamentphp/demo/blob/964d38a387a2e31bde366cccbf09fafb7e38d68a/app/Filament/Resources/Shop/OrderResource.php#L46-L63
While this example isn't calling things from another resource/trait/etc, it IS populating sections of the form from other static methods in the class. This makes them reusable elsewhere, in whatever order or conditions you desire.
GitHub
demo/app/Filament/Resources/Shop/OrderResource.php at 964d38a387a2e...
Source code for the demo.filamentphp.com website. Contribute to filamentphp/demo development by creating an account on GitHub.