英文:
Laravel Yajra Datatable Crashed While Loading Huge Data
问题
我在使用Laravel Yajra Datatable加载172,425条记录时遇到了问题,出现以下错误:
PHP致命错误:在vendor/laravel/framework/src/Illuminate/Database/Connection.php的第421行尝试分配4096字节时,已耗尽允许的内存大小134,217,728字节。
我已经尝试将php.ini中的内存限制从128M设置为512M,然后使用artisan命令清除缓存和配置,但什么都没有改变。
以下是我的代码:
表格
<div class="table-responsive">
<table class="table table-striped" id="table_employee">
<thead>
<tr>
<th>No</th>
<th>Action</th>
<th>API</th>
<th>Description</th>
<th>Cors</th>
<th>Link</th>
</tr>
</thead>
<thead>
<tr>
<th></th>
<th></th>
<th class="th">API</th>
<th class="th">Description</th>
<th class="th">Cors</th>
<th class="th">Link</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
我的Datatable的jQuery代码
$(document).ready(function () {
$('#table_employee').DataTable({
processing : true,
serverSide : true,
responsive : true,
dom : 'Bfrtip',
initComplete: function() {
this.api().columns().every(function() {
var that = this;
$('input', this.footer()).on('keyup change clear', function() {
if (that.search() !== this.value) {
that.search(this.value).draw();
}
});
});
},
ajax: "/api/getdata",
columns: [
{data : 'DT_RowIndex', name : 'DT_RowIndex'},
{data : 'action', name : 'action', orderable: false, searchable: false},
{data : 'API', name : 'API'},
{data : 'Description', name : 'Description'},
{data : 'Cors', name : 'Cors'},
{data : 'Link', name : 'Link'},
]
});
$('#table_employee thead .th').each(function() {
var title = $(this).text();
$(this).html('<input type="text" class="form-control rounded shadow" placeholder="search" />');
});
});
这是我的控制器:
function getdata(){
$data = DB::table('royalti.testapi')->get();
return DataTables::of($data)->addIndexColumn()
->addColumn('action', function($row){
$btn = '<a href="javascript:void(0)" class="btn btn-info text-white btn-sm">' . $row->Cors . '</a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
我使用自己的个人笔记本电脑,具有8GB的内存来运行这个程序。任何帮助都将不胜感激。
英文:
I'm having trouble using Laravel Yajra Datatable to load 172.425 record with the following error:
> PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 4096 bytes) in vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 421.
I already tried to set my php ini memory limit from 128M to 512M then clear my cache and config using artisan command but nothing changes.
here's my code:
The Table
<div class="table-responsive">
<table class="table table-striped" id="table_employee">
<thead>
<tr>
<th>No</th>
<th>Action</th>
<th>API</th>
<th>Description</th>
<th>Cors</th>
<th>Link</th>
</tr>
</thead>
<thead>
<tr>
<th></th>
<th></th>
<th class="th">API</th>
<th class="th">Description</th>
<th class="th">Cors</th>
<th class="th">Link</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
My JQuery for the Datatable
$(document).ready(function () {
$('#table_employee').DataTable({
processing : true,
serverSide : true,
responsive : true,
dom : 'Bfrtip',
initComplete: function() {
this.api().columns().every(function() {
var that = this;
$('input', this.footer()).on('keyup change clear', function() {
if (that.search() !== this.value) {
that.search(this.value).draw();
}
});
});
},
ajax: "/api/getdata",
columns: [
{data : 'DT_RowIndex', name : 'DT_RowIndex'},
{data : 'action', name : 'action', orderable: false, searchable: false},
{data : 'API', name : 'API' },
{data : 'Description', name : 'Description'},
{data : 'Cors', name : 'Cors' },
{data : 'Link', name : 'Link' },
]
});
$('#table_employee thead .th').each(function() {
var title = $(this).text();
$(this).html('<input type="text" class="form-control rounded shadow" placeholder="search" />');
});
});
and here's my controller:
function getdata(){
$data = DB::table('royalti.testapi')->get();
return DataTables::of($data)->addIndexColumn()
->addColumn('action', function($row){
$btn = '<a href="javascript:void(0)" class="btn btn-info text-white btn-sm">' . $row->Cors . '</a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
I run this program using my own personal laptop with 8GB of RAM. Any help would be appreciated.
答案1
得分: 2
你正在使用get()
,因此你的控制器首先获取所有数据,然后将数据发送到前端的yajra/datatable。
你可以这样做(在前端启用paging:true):
$model = ModelName::query();
$dt = new DataTables();
return $dt->eloquent($model)->toJson();
paging:true
仅获取用于在数据表分页上显示的数据。如果要使用数据库门面,请像这样操作:
$users = DB::table('tableName');
$dt = new DataTables();
return $dt->query($users)->toJson(); //datatables将处理其余部分
英文:
You're doing get() so your controller fetching all the data first and then sending the data into yajra/datatable on frontend.
you can do it like this(enable paging:true in dt on frontend):
$model = ModelName::query();
$dt = new DataTables();
return $dt->eloquent($model)->toJson();
paging:true fetches only data to be displayed on datatable pagination.
Do it like this for using db facades:
$users = DB::table('tableName');
$dt = new DataTables();
return $dt->query($users)->toJson(); //datatables will do the rest
答案2
得分: 1
加载所有记录并不是明智的做法。
为了提高性能,最好使用分页功能。
此外,您还可以通过减少返回的列来减少数据量。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论