Laravel-datatables: How not to render all records from database

Created on 16 Jul 2020  路  5Comments  路  Source: yajra/laravel-datatables

Summary of problem or feature request

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!

question

All 5 comments

@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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SGarridoDev picture SGarridoDev  路  3Comments

ahmadbadpey picture ahmadbadpey  路  3Comments

nasirkhan picture nasirkhan  路  3Comments

Abdulhmid picture Abdulhmid  路  3Comments

ghost picture ghost  路  3Comments