I have a database table with around 30 000 records, which should be displayed in DataTable. The issue with the current implementation is that this is a very slow operation and took around 40 seconds to load when I navigate between different pages.
Here is the current implementation at backend:
public function datatables()
{
$selectable = ['id','sku', 'name', 'type', 'price', 'slug', 'status'];
$datas = Product::where('product_type','=','normal')->select($selectable)->orderBy('id','desc')->get();
return Datatables::of($datas)
->editColumn('name', function(Product $data) {
//some code here
})
->editColumn('price', function(Product $data) {
//some code here
})
->addColumn('status', function(Product $data) {
//some code here
})
->addColumn('action', function(Product $data) {
//some code here
})
->rawColumns(['name', 'status', 'action'])
->toJson(); //--- Returning Json Data To Client Side
}
As this is very slow, I decide to change the code to:
public function datatables(Request $request)
{
$start = $request->input('start');
$length = $request->input('length');
$selectable = ['id','sku', 'name', 'type', 'price', 'slug', 'status'];
$datas = Product::where('product_type','=','normal')->select($selectable)->orderBy('id','desc')->skip($start)->take($length)->get();
//--- Integrating This Collection Into Datatables
return Datatables::of($datas)
->editColumn('name', function(Product $data) {
//some code here
})
->editColumn('price', function(Product $data) {
//some code here
})
->addColumn('status', function(Product $data) {
//some code here
})
->addColumn('action', function(Product $data) {
//some code here
})
->rawColumns(['name', 'status', 'action'])
->toJson(); //--- Returning Json Data To Client Side
}
Now its much faster, but the issue is that now the number of pages in html disappeared.
So, my question is how to pass just 10 records from the backend, but also pass overall records count, so pages to render properly.
Thanks!
@abcmedia-bg can you keep the original query and just get rid of the get() method. That should improve your rendering speed considerably.
Yes, just remove ->get().
Thanks, guys!!! This works perfectly fine! Can someone explain a bit, why ->get() cause this issue?
Thanks, guys!!! This works perfectly fine! Can someone explain a bit, why ->get() cause this issue?
The get() method loads all your data into the collection, which depending on how large the dataset is, may consume a lot of memory.
Thanks again!