使用Yajra数据表的Laravel Raw查询,数据表服务器端Pagianation不起作用

时间:2019-01-10 18:58:53

标签: laravel datatable pagination server-side yajra-datatable

我的自定义查询是

$statistics_sql = "SELECT  l.*
FROM    (SELECT * FROM DB1.dept ORDER BY parent_id, id) d, 
(SELECT @pid := 2058) initialisation, DB1.users u, DB2.feedb_list l
WHERE   (find_in_set (d.parent_id, @pid) > 0 OR find_in_set (d.id, @pid) > 0)
AND     length(@pid := concat(@pid, ',', d.id))
AND     u.department_id=d.id
AND     l.uid=u.id
AND     u.deleted_at IS NULL ";

$feedbacks  = DB::select(DB::raw($statistics_sql));
$feedbacks  = collect($feedbacks);

return Datatables::of($feedbacks)->make(true);

JS脚本

var datatable = $('#list').DataTable({
    lengthMenu: [
        [ 10, 25, 50, 100 ],
        [ '10', '25', '50','100']
    ],
    "pageLength": 50,
    "pagingType": "full_numbers",
    dom: 'lBfrtip',
    processing: true,
    serverSide: true,
    "searching": false,
    //stateSave: true,
    ajax: {
        url:  '{!! route('all-list-ajax') !!}',
        type: 'post',
        "data":function (d) {
            //....
        }
     },
    columns: [
        { .... }
    ]
});

Yajra Datatable服务器端分页无法通过上述方式进行工作,即如何使用自定义查询对Datatable进行正确的分页。

1 个答案:

答案 0 :(得分:0)

尝试删除集合:

$statistics_sql = "SELECT  l.*
FROM    (SELECT * FROM DB1.dept ORDER BY parent_id, id) d, 
(SELECT @pid := 2058) initialisation, DB1.users u, DB2.feedb_list l
WHERE   (find_in_set (d.parent_id, @pid) > 0 OR find_in_set (d.id, @pid) > 0)
AND     length(@pid := concat(@pid, ',', d.id))
AND     u.department_id=d.id
AND     l.uid=u.id
AND     u.deleted_at IS NULL ";

$feedbacks  = DB::select(DB::raw($statistics_sql));
//$feedbacks  = collect($feedbacks);

// FOR YAJRA DATATABLES VERSION > 8.0
return Datatables::of($feedbacks)->toJson();
// FOR YAJRA DATATABLES VERSION < 7.0
return Datatables::of($feedbacks)->make(true);