Relationships

Out of the box the columns support hasOne, belongsTo, and MorphOne relationships for display, sorting, and searching. The component will automatically join the necessary tables.

To call these relationships, just use the relationship dot-notation string as the field name:

1protected $model = User::class;
2 
3// ...
4 
5public function columns(): array {
6 return [
7 // Looks for the address column on the address relationship of User.
8 // $user->address->address
9 Column::make('Address', 'address.address'),
10 
11 // Looks for $user->address->group->name
12 Column::make('Address Group', 'address.group.name'),
13 
14 // Looks for $user->address->group->city->name
15 Column::make('Group City', 'address.group.city.name'),
16 ];
17}

The above will join the necessary tables as well as alias the columns for selecting, sorting, searching, etc.:

1SELECT `addresses`.`address` AS `address.address`,
2 `address_groups`.`name` AS `address.group.name`,
3 `cities`.`name` AS `address.group.city.name`
4FROM `users`
5 LEFT JOIN `addresses`
6 ON `addresses`.`user_id` = `users`.`id`
7 LEFT JOIN `address_groups`
8 ON `addresses`.`address_group_id` = `address_groups`.`id`
9 LEFT JOIN `cities`
10 ON `address_groups`.`city_id` = `cities`.`id`