i have an index function that loads the data by time i have more rows in my database and i need to get them all but the loading it takes time like 8 seconds for getting 400 rows i have currently and it will be more in the future how i can optimize the loading speed without using pagination because i am using datatables here is my code:
RdvsController.php
public function index()
{
$users = User::all();
$user = Auth::user();
// Superadmin sees all RDVs
if ($user->isSuperAdmin() || $user->isSuppervisor()) {
$rdvs = Rdvs::with('user')->orderBy('created_at', 'desc')->get();
// Admin sees only RDVs they created (created_by = their ID)
} elseif ($user->isAdmin()) {
$rdvs = Rdvs::with('user')->where('created_by', $user->id)->orderBy('created_at', 'desc')->get();
// All other roles get nothing or access denied
} else {
abort(403, 'Unauthorized access');
}
return view('rdvs.index', compact('rdvs', 'users'));
}
Datatables.js
// var e;
c1 = $('#style-1').DataTable({
headerCallback: function(e, a, t, n, s) {
e.getElementsByTagName("th")[0].innerHTML=`
<div class="form-check form-check-primary d-block">
<input class="form-check-input chk-parent" type="checkbox" id="form-check-default">
</div>`
},
columnDefs: [
{
targets: 0,
width: "30px",
className:"",
orderable: !1,
render: function(e, a, t, n) {
return `
<div class="form-check form-check-primary d-block">
<input class="form-check-input child-chk" type="checkbox" id="form-check-default">
</div>`
}
}
],
"dom": "<'dt--top-section'<'row'<'col-12 col-sm-6 d-flex justify-content-sm-start justify-content-center'l><'col-12 col-sm-6 d-flex justify-content-sm-end justify-content-center mt-sm-0 mt-3'f>>>" +
"<'table-responsive'tr>" +
"<'dt--bottom-section d-sm-flex justify-content-sm-between text-center'<'dt--pages-count mb-sm-0 mb-3'i><'dt--pagination'p>>",
"oLanguage": {
"oPaginate": {
"sPrevious": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-left"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>',
"sNext": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-right"><line x1="5" y1="12" x2="19" y2="12"></line><polyline points="12 5 19 12 12 19"></polyline></svg>' },
"sInfo": "Showing page _PAGE_ of _PAGES_",
"sSearch": '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>',
"sSearchPlaceholder": "Search...",
"sLengthMenu": "Results : _MENU_",
},
"lengthMenu": [5, 10, 20, 50],
"pageLength": 10
}
);
multiCheck(c1);
$usersvariable used for in the blade? Using theall()method will lead to the slow query if users data accumulates. 2. As mentionedby @KIKOSoftware, it's better to use pagination when trying to retrieve all data. You'll need knowledge in ajax before doing server-side processing for Datatables.