Reputation: 134
I have a huge record stored in a database that is taking time to load, so I thought about using yajra laravel datatables. I am using laravel version 7 with Yajra Datatables 9.18.
So, only 10 records must come from the database on the first page load after that when I click on the next button next 10 records should come.
Here is my code
Backend
consider this line 1
$vouchers = Voucher::where('status', 'InActive')->orderBy('id', 'desc')->get(); // more than 800000 entries
At last, after looping I return this as
return Datatables::of($dataFinal)->make(true);
Frontend
processing: true,
serverSide: true,
ajax: "{{ route('vouchers_report') }}",
I checked many answers on stack overflow and some other platforms but everybody is asking me to use
"pageLength": 10,
But here the problem is with line 1 because I am getting all the entries at one time. It is taking too much time to load then there is no logic to use pageLength because it does not reach from backend to frontend.
I tried a lot of options but that is not working at all.
Upvotes: 1
Views: 2625
Reputation: 2814
You should give the query builder to Datatables instead of the model collection.
Try
$vouchers = Voucher::where('status', 'InActive')->orderBy('id', 'desc');
return Datatables::of($vouchers)->make(true);
Note that I left out the ->get()
part.
Even better would be to let Datatables also control the sorting part, but that's step 2.
Upvotes: 3