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?
5 Replies
awcodes
awcodes2w 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_OP7d 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
awcodes7d 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_OP5d 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
awcodes5d 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.
Want results from more Discord servers?
Add your server