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
LeandroFerreira
LeandroFerreira5mo ago
you mean, this?
TextInput::configureUsing(function (TextInput $input) {
$input->label("Your {$input->getLabel()}");
});
TextInput::configureUsing(function (TextInput $input) {
$input->label("Your {$input->getLabel()}");
});
global settings
BigBlueMonkey
BigBlueMonkeyOP5mo ago
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?
public function getLabel(): string
{
$label = $this->evaluate($this->label) ?? (string) str($this->getName())
->before('.')
->kebab()
->replace(['-', '_'], ' ')
->ucfirst();

return $this->shouldTranslateLabel ? __($label) : $label;
}
public function getLabel(): string
{
$label = $this->evaluate($this->label) ?? (string) str($this->getName())
->before('.')
->kebab()
->replace(['-', '_'], ' ')
->ucfirst();

return $this->shouldTranslateLabel ? __($label) : $label;
}
The code in trait HasLabel suggests there is no way to do this cleanly?
ChesterS
ChesterS5mo ago
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
public function mount(int|string $record): void
{
TextInput::configureUsing(function (TextInput $component) {
$component->label("Sponge Cake");
});

parent::mount($record);
}
public function mount(int|string $record): void
{
TextInput::configureUsing(function (TextInput $component) {
$component->label("Sponge Cake");
});

parent::mount($record);
}
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)
BigBlueMonkey
BigBlueMonkeyOP5mo ago
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 😛
Arnold Schwarzenegger
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?
Martin Oscar
Martin Oscar5mo ago
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 mean
BigBlueMonkey
BigBlueMonkeyOP5mo ago
Ideally I have a model, and in that model I have all the attributes. Eg:
$attrubutes = [
'id',
'title',
'color',
'balance'
];
$attrubutes = [
'id',
'title',
'color',
'balance'
];
And then I have a lables array which updates the labels whenever this model is called
$labels= [
'id' => 'Document ID',
'title' => 'Document title',
'color' => 'Color of the ocean',
'balance' => 'Your account balance'
];
$labels= [
'id' => 'Document ID',
'title' => 'Document title',
'color' => 'Color of the ocean',
'balance' => 'Your account balance'
];
and then when I call
TextColumn::make('color')
TextColumn::make('color')
I no longer need to specify
->label(Color of the ocean)
->label(Color of the ocean)
anymore. And the same would be applicable to forms, lists, etc etc
ChesterS
ChesterS5mo ago
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
//YourModel.php
public function getLabels() {
return [
'id' => 'Document ID',
'title' => 'Document title',
'color' => 'Color of the ocean',
'balance' => 'Your account balance'
];
}
//YourModel.php
public function getLabels() {
return [
'id' => 'Document ID',
'title' => 'Document title',
'color' => 'Color of the ocean',
'balance' => 'Your account balance'
];
}
- Use Leandro's suggestion to do something like
Field::configureUsing(function (Field $component) {
$component->label(fn(Model $record) => method_exists($reocrd, 'getLabels') ? $record->getLabels()[$component->getName()] : $component->getLabel();
});
Field::configureUsing(function (Field $component) {
$component->label(fn(Model $record) => method_exists($reocrd, 'getLabels') ? $record->getLabels()[$component->getName()] : $component->getLabel();
});
- 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.
BigBlueMonkey
BigBlueMonkeyOP4mo ago
Appreciate it - thank you

Did you find this page helpful?