Laravel 按关系列排序的数据表不起作用

Laravel Datatables order by relationship column does not work

我有一个User可以分配给很多Company。我正在尝试使用 Laravel Datatables 和 jQuery Datatables 渲染 table。它呈现得很好,当单击 table header 中的顺序图标时,它会按该列对数据进行排序,但它不适用于关系列 company_name。 这是我在控制器中的代码:

$users = User::with(['roles','companies'])
        ->where('users.id', '!=', Auth::id())
        ->whereHas('roles', function($q){$q->whereId(Role::ROLE_6);});

...

return Datatables::of($users)
->editColumn('company', function (User $user) {
                return $user->hasCompanies()? $user->companies->first()->company_name : trans('lang.company.not_assigned');
            })
->orderColumn('company', 'company')
->make(true);

这是我的 javascript 数据 tables:

otable = $('#datatable_fixed').DataTable({
                "ajax": {
                    url: 'users/datatable',
                    type: 'POST',
                },
                "pageLength": 15,
                "processing": true,
                "stateSave": true,
                "serverSide": true,
                "bDestroy": true,
                columns: [
                    {data: 'first_name', name: 'first_name'},
                    {data: 'last_name', name: 'last_name'},
                    {data: 'company', name: 'company.company_name'},
                    {data: 'email', name: 'email'},
                    {data: 'status', name: 'status'},
                ],
                dom: 'Bfrtip',
                searching: false,
                "order": [[0, 'asc']],
                "autoWidth": true,
            });

我有一个名为 rides 的 table,它与 driverscustomersvehiclescompanies 有多种关系。我需要一列包含两个关系 table 列的串联值,这些列可以是 sortable 和可搜索的。

此查询为我完成上述工作

$rides = Ride::select(['rides.*', DB::raw('CONCAT(drivers.code," - ",drivers.name) AS driver')])
    ->with(['drivers','customers','vehicles','companies'])
    ->join('drivers','rides.driver_id','=', 'drivers.id');

在此之后,我将下面的代码添加到这个 ajax 加载数据 table 数据的方法中。

var table = $('#myTable').DataTable({
    "processing":true,
    "serverSide":true,
    "ajax": "{{route('ajax.view.rides')}}",
    "columns": [
        {data: 'driver', name: 'driver', searchable:false},
        {data: 'drivers.code', name:'drivers.code', searchable: true, sortable : true, visible:false},
        {data: 'drivers.name', name:'drivers.name', searchable: true, sortable : true, visible:false},
       
    ],
    responsive:true,
    order:[0,'desc']
});

我们在查询中添加的串联列使排序成为可能

{data: 'driver', name: 'driver', searchable:false},

此列定义将使搜索成为可能与两列

{data: 'drivers.code', name:'drivers.code', searchable: true, sortable : true, visible:false},
{data: 'drivers.name', name:'drivers.name', searchable: true, sortable : true, visible:false},

现在您需要在 HTML table 线程元素中添加两个额外的 <th> 标签。

<thead>
  <tr>
    <th>Driver</th> <!-- this one will display the concatenated column -->
    <th>Driver</th> <!-- this one is for the hidden hidden column that enables the search on one column -->
    <th>Driver</th> <!-- this one is for the hidden hidden column that enables the search on one column -->
  </tr>
</thead>

这对我有用

$model = Expenses::with('employee')
    ->where('bookingoffice_id', Auth::user()->bookingoffice)
    ->where('capitalsmb.expenses.active', 1)
    ->select('expenses.*');