Same field in all forms/resources

Let's say we have a Select Field for selecting company in 90% of our forms. Same for table column. Is there anyway to avoid repeating the code. Either by doing some CompanyForm that already has it in the schema, somehow using a trait, making a custom Resource such as CompanyResource which we extend. I cannot really find the best or most appropriate solution for this. Anyone experienced anything similar and have any suggestions on how to best solve it?
6 Replies
awcodes
awcodes2mo ago
Just create a CompanySelect custom form component that extends the Select and use that in your forms instead. In the setUp method you can define all the field modifiers.
prouse_
prouse_OP2mo ago
Ideally we want to avoid needing to add the component into each form and then also the column into each table. Would like to achieve it in a more elegant way. For example like how you can use traits in Laravel models and boot into it. Just adding a trait to the resource. Or extend CompanyResource or just add a property to the Resource. I can't find any way though without overriding and changing too much of the core. @awcodes do you have any way in mind?
awcodes
awcodes2mo ago
what's the difference in adding a trait to all the resources and adding a Custom field to all the forms? i don't see the advantage of one over the other.
prouse_
prouse_OP2mo ago
I think it's more than just adding to all the forms. Because all resources that have this also needs to add in the table, and then also in the filters would be preferable. That's 3 places per resource now at least where we need to repeat the same code. So in that sense a trait or being able to extend a specific class would make it much cleaner.
awcodes
awcodes2mo ago
Fair, but you’re still going to have to merge in any other fields or columns even with overriding the methods you would have to merge with the parent anyway. In my head, including a custom form field and a custom table column is easier and more extendable than overriding backwards. Whatever works for you though. I could very well be missing the point. I would prefer opting in over opting out.
prouse_
prouse_OP5w ago
Yeah, between these two options I agree with you. What I'm looking for is a solution similar to how you can use a bootable trait in laravel models to add functionality to your model without needing to touch the rest of the model. Basically hooking into the form/table using the trait. Or some other similar solution. But I guess that is currently not possible with Filament.. 😞
Want results from more Discord servers?
Add your server