如何为以下情况编写背包OrderLogic

问题描述 投票:0回答:1

我有三个表,下面列出了重要的列。

users table
-id
-name
-tenant_id

tenants table
-id
-name
-agent_team_id
-client_team_id

teams table
-id
-name
  • tenant_id是租户表的外键。
  • agent_team_id,client_team_id是团队表的外键。

我的用户列表列包含座席团队和客户团队列。如何使此列可排序?

为类似的列添加列逻辑:

$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 : '-';
            },
]);
laravel backpack-for-laravel
1个回答
0
投票

尝试一下,它将起作用。更多参考:Documentation

$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);
             },
]);
© www.soinside.com 2019 - 2024. All rights reserved.