【发布时间】:2020-09-02 06:15:21
【问题描述】:
使用 Yajra Datables,Laravel 正在返回“内存耗尽...”错误消息。 实际上,我们如何将过滤和排序直接应用于 SQL,而不是让服务器端处理过滤和排序。
Laravel 控制器
// Table1 has 200,000 rows, the query itself is also slow
$rs = DB::select("select *
from Table1");
$numrow = count($rs);
if ($numrow > 0) {
foreach ($rs as $row) {
$column1 = $row_customers->column1;
$arr['col1'] = $column1;
$arrs[] = $arr;
}
}
$response = Datatables::of($arrs)->make(true)->getData(true);
return response()->json($response);
Laravel 视图
$('#Table1').DataTable({
processing: true,
serverSide: true,
responsive: true,
cache: false,
ajax: {
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
"url": "/GetTable1",
"type": "POST",
error: function(jqXHR, ajaxOptions, thrownError) {
console.log('error.jqXHR', jqXHR);
console.log('error.ajaxOptions', ajaxOptions);
console.log('error.thrownError', thrownError);
}
}
/* ... */
});
【问题讨论】:
-
最好的方法是使用分页,不要查询所有行,而是按块显示记录,如延迟加载或其他内容
标签: laravel datatable yajra-datatable