具有复杂连接的Laravel数据表无法正常工作

时间:2017-06-10 22:37:31

标签: laravel datatables

我正在使用yajra laravel数据表来填充我的数据表。

我有一个非常复杂的查询,包括各种连接:

.full-width

我有我的javascript:

$activityList = DB::table('activities')
    ->select( 'u2.id as manager_id','u2.name as manager_name','u.id as user_id','u.name as user_name','p.id as project_id','p.project_name as project_name','p.customer_name as customer_name','activities.year as year')

->leftjoin('projects as p', 'p.id', '=', 'activities.project_id')
->leftjoin('users as u', 'u.id', '=', 'activities.user_id')
->leftjoin('users_users as uu', 'u.id', '=', 'uu.user_id')
->leftjoin('users AS u2', 'u2.id', '=', 'uu.manager_id');

  $data = Datatables::of($activityList)->make(true);
  return $data;

我无法使用服务器端。

如果我不使用服务器端,一切正常,但是一旦我设置了服务器端,我就会获得所有数据并限制记录数量,搜索,排序不再有效。

activitiesTable = $('#activitiesTable').DataTable({
                scrollX: true,
                serverSide: true,
                processing: true,
                ajax: {
                        url: "{!! route('listOfActivitiesPerUserAjax') !!}",
                        type: "POST",
                        data: ajaxData,
                        dataType: "JSON"
                    },
                columns: [
                    { name: 'u2.id', data: 'manager_id' , searchable: false , visible: false},
                    { name: 'u2.name', data: 'manager_name', width: '150px' },
                    { name: 'u.id', data: 'user_id' , searchable: false , visible: false},
                    { name: 'u.name', data: 'user_name' , width: '150px'},
                    { name: 'p.customer_name', data: 'customer_name' , width: '200px'},
                    { name: 'p.id', data: 'project_id' , searchable: false , visible: false},
                    { name: 'p.project_name', data: 'project_name', width: '200px'},
                    { name: 'activities.year', data: 'year' , searchable: false , visible: false},
                    {
                        name: 'actions',
                        data: null,
                        sortable: false,
                        searchable: false,
                        render: function (data) {
                            var actions = '';
                            return actions;
                        }
                    }
                    ],
                order: [[2, 'asc']],

你可以看到最后,它没有添加限制或偏移参数。

1 个答案:

答案 0 :(得分:0)

在datatables演示应用程序中,连接发生在select语句之前。我无法确定是否会导致问题,因为我从未将数据表与流畅的查询构建器一起使用,但可能值得研究。请参阅此处的示例代码:

https://datatables.yajrabox.com/fluent/joins

基本服务实施:

https://datatables.yajrabox.com/services/basic