0

In my plugin I have two models:

  • Manager
  • Office

In office model I have relation:

public $hasMany = [
    'managers'  => [Manager::class, 'key' => 'office_id'],
];

Each manager belong to some office and related to backend user:

public $belongsTo = [
    'backend_user' => [BackendUser::class],
    'office' => [Office::class]
];

In Office form I have relation widget and want to use full name as nameFrom value:

fields:
    managers:
        type: relation
        label: Managers
        nameFrom: full_name

But full name (fisrt name and last name) is stored in backend user:

$first_name = $manager->backend_user->first_name;
$last_name = $manager->backend_user->last_name;

The idea was to create a mutator in manager model:

public function getFullNameAttribute() 
{
    return $this->backend_user->first_name.' '.$this->backend_user->last_name;
}

But in this case name gets from first model, not from each, and I got all checkboxes with same name.

How can I achive that?

1 Answers1

0

Use select:

fields:
    managers:
        type: relation
        label: Managers
        select: concat(first_name,' ',last_name)

This will work with a bunch of the available MySQL aggregates.

https://octobercms.com/docs/backend/forms#widget-relation

MHewison
  • 846
  • 10
  • 17