Laravel/DataTable 无法显示约 80,000 行
Laravel/DataTable fails at displaying ~80,000 rows
我正在尝试将 DataTables 与服务器端处理一起使用以显示 80,000 行(分页),但 Laravel 崩溃并出现以下错误:
PHP Fatal error: Maximum execution time of 300 seconds exceeded in /Users/fin/Documents/dev/evway-new/vendor/nesbot/carbon/src/Carbon/Carbon.php on line 291
.
这是我在其中获取行的控制器方法:
public function data()
{
$employees = Employee::get(); // ~80,000 employees
return Datatables::of($employees)
->edit_column('created_at',function(Employee $employee) {
return $employee->created_at->diffForHumans();
})
->add_column('actions',function($employee){
$actions = '<a href='. route('admin.employees.show', $employee->id) .'><i class="livicon" data-name="info" data-size="18" data-loop="true" data-c="#428BCA" data-hc="#428BCA" title="view employee"></i></a>
<a href='. route('admin.employees.edit', $employee->id) .'><i class="livicon" data-name="edit" data-size="18" data-loop="true" data-c="#428BCA" data-hc="#428BCA" title="update employee"></i></a>';
$actions .= '<a href='. route('admin.employees.clone', $employee->id) .' data-target="#clone"><i class="livicon" data-name="plus" data-size="18" data-loop="true" data-c="#f56954" data-hc="#f56954" title="clone employee"></i></a>';
$actions .= '<a href='. route('admin.employees.confirm-delete', $employee->id) .' data-toggle="modal" data-target="#delete_confirm"><i class="livicon" data-name="remove" data-size="18" data-loop="true" data-c="#f56954" data-hc="#f56954" title="delete employee"></i></a>';
return $actions;
}
)
->make(true);
}
这是我配置 DataTable 的 JS:
$(function() {
var table = $('#table').DataTable({
processing: true,
serverSide: true,
ajax: '{!! route('admin.employees.data') !!}',
columns: [
{ data: 'id', name: 'id' },
{ data: 'name', name: 'name' },
{ data: 'status', name: 'status' },
{ data: 'actions', name: 'actions', orderable: false, searchable: false }
]
});
});
我做错了什么?我是否以错误的方式从数据库中获取员工? get()
是个坏主意吗?
我认为你应该增加你的 php.ini
max_execution_time = 360 ; Maximum execution time of each script, in seconds (I CHANGED THIS VALUE)
max_input_time = 120 ; Maximum amount of time each script may spend parsing request data
;max_input_nesting_level = 64 ; Maximum input variable nesting level
memory_limit = 128M
我还没有真正使用过数据表插件,但你的第一句话就是罪魁祸首。 $employees = Employee::get(); // ~80,000 employees
.. 这会将所有 80k 条记录加载到内存中。然后将此集合传递给 Datatables::of()
方法。而不是这个我认为你应该使用 Datatables::of(Employee::query())
,我假设它会处理分页并且只会加载所需数量的员工 –
我正在尝试将 DataTables 与服务器端处理一起使用以显示 80,000 行(分页),但 Laravel 崩溃并出现以下错误:
PHP Fatal error: Maximum execution time of 300 seconds exceeded in /Users/fin/Documents/dev/evway-new/vendor/nesbot/carbon/src/Carbon/Carbon.php on line 291
.
这是我在其中获取行的控制器方法:
public function data()
{
$employees = Employee::get(); // ~80,000 employees
return Datatables::of($employees)
->edit_column('created_at',function(Employee $employee) {
return $employee->created_at->diffForHumans();
})
->add_column('actions',function($employee){
$actions = '<a href='. route('admin.employees.show', $employee->id) .'><i class="livicon" data-name="info" data-size="18" data-loop="true" data-c="#428BCA" data-hc="#428BCA" title="view employee"></i></a>
<a href='. route('admin.employees.edit', $employee->id) .'><i class="livicon" data-name="edit" data-size="18" data-loop="true" data-c="#428BCA" data-hc="#428BCA" title="update employee"></i></a>';
$actions .= '<a href='. route('admin.employees.clone', $employee->id) .' data-target="#clone"><i class="livicon" data-name="plus" data-size="18" data-loop="true" data-c="#f56954" data-hc="#f56954" title="clone employee"></i></a>';
$actions .= '<a href='. route('admin.employees.confirm-delete', $employee->id) .' data-toggle="modal" data-target="#delete_confirm"><i class="livicon" data-name="remove" data-size="18" data-loop="true" data-c="#f56954" data-hc="#f56954" title="delete employee"></i></a>';
return $actions;
}
)
->make(true);
}
这是我配置 DataTable 的 JS:
$(function() {
var table = $('#table').DataTable({
processing: true,
serverSide: true,
ajax: '{!! route('admin.employees.data') !!}',
columns: [
{ data: 'id', name: 'id' },
{ data: 'name', name: 'name' },
{ data: 'status', name: 'status' },
{ data: 'actions', name: 'actions', orderable: false, searchable: false }
]
});
});
我做错了什么?我是否以错误的方式从数据库中获取员工? get()
是个坏主意吗?
我认为你应该增加你的 php.ini
max_execution_time = 360 ; Maximum execution time of each script, in seconds (I CHANGED THIS VALUE)
max_input_time = 120 ; Maximum amount of time each script may spend parsing request data
;max_input_nesting_level = 64 ; Maximum input variable nesting level
memory_limit = 128M
我还没有真正使用过数据表插件,但你的第一句话就是罪魁祸首。 $employees = Employee::get(); // ~80,000 employees
.. 这会将所有 80k 条记录加载到内存中。然后将此集合传递给 Datatables::of()
方法。而不是这个我认为你应该使用 Datatables::of(Employee::query())
,我假设它会处理分页并且只会加载所需数量的员工 –