Skip to content
Discussion options

You must be logged in to vote

The first idea that comes to my mind is to use an accessor together with the attribute property.
You can do something like this instead of the current name

'attribute' => 'name_translated'

and then in your model add the accessor like this:
Laravel 9:

protected function nameTranslated(): Attribute
{
    return Attribute::make(
        get: fn ($value) => __($this->name),
    );
}

Laravel 8:

public function getNameTranslatedAttribute($value)
{
    return __($this->name);
}

Most probably you will have to customise the translation function __($value) to your needs.
If you think that in all places where you read the name you would like it translated, then you could just make the accessor for

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@cod3rshotout
Comment options

@o15a3d4l11s2
Comment options

@cod3rshotout
Comment options

@o15a3d4l11s2
Comment options

Answer selected by cod3rshotout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants