Skip to content

Relation fromModel custom name #2752

Answered by tabuna
m-pastuszek asked this question in Q&A
Discussion options

You must be logged in to vote

You can add a calculated attribute to your model, for example

class Workplace extends Model
{
    /**
     * @return string
     */
    public function getFullNameAttribute(): string
    {
        return $model->department->name . ' - ' . $model->name;
    }
}

Then use the displayAppend method to specify which calculated field to use for display:

Relation::make('workplace.')
    ->fromModel(Workplace::class, 'name')
    ->displayAppend('fullName')
    ->multiple()
    ->title('Select workplace');

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@m-pastuszek
Comment options

Answer selected by m-pastuszek
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants