Can you set field labels in the model or resource?
I am sure this is going to be a stupid question but I find myself having to write the labels for fields over and over (eg in the form, the table, etc). Is there no way to set the labels for a field once in the resource and then have them automatically applied rather than having to use ->label over and over?
Eg for TextInput::make('title'), can I specify either in the model or the resource that 'title' has a label of 'Your Title' rather than constantly having to do TextInput::make('title')->label('Your Title')
9 Replies
Thank you for the response - no that's not really what I was thinking because $input->getLabel() is still going to return "Title". So perhaps to make it more obvious lets assume I want a 'title' field to be labeled as 'Sponge Cake' everywhere in that resource - is there no simple way to define the value that ->getLabel() pulls from?
The code in trait HasLabel suggests there is no way to do this cleanly?
You sort of can by adding @Leandro Ferreira 's (sorry for the ping) answer to the page (not the resource)
So in your
EditRecord
page you can add something like this
this will change all labels in that form to Honey Badger
Not sure if this is a good idea though. Up to you ¯\_(ツ)_/¯
disclaimer : not sure if this will have any unforseen side effects - haven't tested it thoroughly
Also, Forms\Components\Field
will apply this to anything that has a label (again, use at your own risk)Fair, but then you are still defining it per page - which really isnt the intent. Ideal scenario is to put an associative array in model. Maybe V4 😛
What exactly are you trying to achieve here? Do you want something that is pretty much a global change or a solution that is per-model? What's your ideal scenario?
You are way better of moving
form()
& table()
into the Resource rather then redefining it in every page here's a clearer example of what i meanIdeally I have a model, and in that model I have all the attributes. Eg:
And then I have a lables array which updates the labels whenever this model is called
and then when I call I no longer need to specify anymore. And the same would be applicable to forms, lists, etc etc
This looks possible, but it's rather involved
- You need to add method on each model you want to have custom labels (I suggest a method instead of an array so you can translate the strings) eg
- Use Leandro's suggestion to do something like
- Either load the above as a trait or apply it globally.
Soooomething like that. I haven't tested any of those and, honestly, it looks like a bad idea. It feels like you're complicating things instead of simplifying them.
Anyway, I haven't tested any of the above but maybe they'll help you.
Appreciate it - thank you