Как написать Рюкзак OrderLogi c для сценария ниже - PullRequest
0 голосов
/ 09 апреля 2020

У меня есть три таблицы, и важные столбцы обозначены ниже.

users table
-id
-name
-tenant_id

tenants table
-id
-name
-agent_team_id
-client_team_id

teams table
-id
-name
  • tenant_id - внешний ключ таблицы tenants.
  • agent_team_id, client_team_id - внешний ключ таблицы команды.

В моем столбце списка пользователей есть столбцы команды агента и команды клиента. Как сделать эти столбцы как заказываемые?

Добавить столбцы Logi c для таких столбцов, как:

$this->crud->addColumns([
           'name'  => 'agent_team_name',
           'label' => 'Agent Team',
           'type' => 'closure',
           'function' => function ($entry) {
                 $team_id = $entry->tenants->agent_team_id;
                 return isset($team_id) ? Team::findOrFail($team_id)->name : '-';
            },
]);
$this->crud->addColumns([
           'name'  => 'client_team_name',
           'label' => 'Client Team',
           'type' => 'closure',
           'function' => function ($entry) {
                 $team_id = $entry->tenants->client_team_id;
                 return isset($team_id) ? Team::findOrFail($team_id)->name : '-';
            },
]);

1 Ответ

0 голосов
/ 09 апреля 2020

Попробуйте, это будет работать. Дополнительная информация: Документация

$this->crud->addColumns([
           'name'  => 'agent_team_name',
           'label' => 'Agent Team',
           'type' => 'closure',
           'function' => function ($entry) {
                 $team_id = $entry->tenants->agent_team_id;
                 return isset($team_id) ? Team::findOrFail($team_id)->name : '-';
            },
            'orderable' => true,
            'orderLogic' => function ($query, $column, $columnDirection) {
                        return $query->join('tenants', 'students.tenant_id', 'tenants.id')
                                    ->join('teams', 'tenants.agent_team_id', 'teams.id')
                                    ->orderBy('teams.name', $columnDirection);
             },
]);
$this->crud->addColumns([
           'name'  => 'client_team_name',
           'label' => 'Client Team',
           'type' => 'closure',
           'function' => function ($entry) {
                 $team_id = $entry->tenants->client_team_id;
                 return isset($team_id) ? Team::findOrFail($team_id)->name : '-';
            },
            'orderable' => true,
            'orderLogic' => function ($query, $column, $columnDirection) {
                        return $query->join('tenants', 'students.tenant_id', 'tenants.id')
                                    ->join('teams', 'tenants.client_team_id', 'teams.id')
                                    ->orderBy('teams.name', $columnDirection);
             },
]);
...