Laravel Server Side Yajra DataTable - Load Large Data Within Few Seconds
Large Data or 1 million Data Quick Load within few seconds in Laravel Server Side DataTables
- Run the following in the command/terminal to create a new laravel project,
- Run the following in the command/terminal to install yajra datatables package,
- Check composer.json
- Add the following code file to config/app.php
- publish the configuration, run the following line in the terminal,
- in .env for database setup
- Add below files for list details in the table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>DataTables Load Large Data's</title> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> | |
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css"> | |
</head> | |
<body> | |
<div class="container"> | |
<h3>Large Data or 1 million Data Quick Load within few second | DataTables Server Processing in Laravel</h3> | |
<br> | |
<table class="table table-striped" id="user_details_table" width="100%"> | |
<thead> | |
<tr> | |
<th>ID</th> | |
<th>User Name</th> | |
<th>First Name</th> | |
<th>Last Name</th> | |
<th>Gender</th> | |
<th>Status</th> | |
</tr> | |
</thead> | |
<tbody> | |
</tbody> | |
</table> | |
</div> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> | |
<script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script> | |
<script type="text/javascript"> | |
$(document).ready(function () { | |
$("#user_details_table").DataTable({ | |
serverSide: true, | |
ajax: { | |
url: '{{route('user.details')}}' | |
}, | |
buttons: false, | |
searching: true, | |
processing: true, | |
scrollY: 500, | |
scrollX: true, | |
scrollCollapse: true, | |
columns: [ | |
{data: "user_id", className: 'user_id'}, | |
{data: "username", className: 'username'}, | |
{data: "first_name", className: 'first_name'}, | |
{data: "last_name", className: 'last_name'}, | |
{data: "gender", className: 'gender'}, | |
{data: "status", className: 'status'} | |
] | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Contracts\Foundation\Application; | |
use Illuminate\Contracts\View\Factory; | |
use Illuminate\Contracts\View\View; | |
use Illuminate\Http\JsonResponse; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\DB; | |
use Yajra\DataTables\DataTables; | |
class UserController extends Controller | |
{ | |
/** | |
* List User Details | |
* @param Request $request | |
* @return Application|Factory|View|JsonResponse | |
*/ | |
public function listUserDetails(Request $request) | |
{ | |
if ($request->ajax()) { | |
$query = DB::table('user_details'); | |
return DataTables::of($query) | |
->editColumn('status', function ($data) { | |
return $data->status == 1 ? 'Active' : 'Inactive'; | |
})->make(true); | |
} | |
return view('user_details'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Route; | |
use App\Http\Controllers\UserController; | |
Route::get('/', function () { | |
return view('welcome'); | |
}); | |
Route::get('/user-list',[UserController::class, 'listUserDetails'])->name('user.details'); |
- Finally, download the below SQL file to import into the database
1 million data SQL file - https://drive.google.com/file/d/1PGvg5hWaQRjEA78iPABpOhYIiwbtS1_w/view?usp=sharing
Note:
Comments
Post a Comment