5

我有一个User可以分配给很多人Company。我正在尝试使用 Laravel Datatables 和 jQuery Datatables 呈现表格。它呈现得很好,当单击表头中的订单图标时,它会按该列对数据进行排序,但它不适用于关系列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:

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,
            });
4

2 回答 2

6

我有一个名为, ,和的rides具有多个关系的表。我需要一个包含两个关系表列的连接值的列,可以排序和搜索。driverscustomersvehiclescompanies

此查询为我完成上述工作

$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 方法中添加了以下代码。

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},

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

<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>
于 2018-10-31T19:44:05.107 回答
0

This works for me

$model = Expenses::with('employee')
    ->where('bookingoffice_id', Auth::user()->bookingoffice)
    ->where('capitalsmb.expenses.active', 1)
    ->select('expenses.*');
于 2020-08-22T00:09:56.083 回答