How to Implement Yajra Datatables Server Side in Laravel 9?

Feb 22, 2022 . Admin

Hi all,

This article will provide some of the most important how to implement yajra datatables server side in laravel 9?. The complete step by step guide of how to use yajra datatables server side in laravel 9?. This Laravel 9 tutorial help to implement yajra datatable server side with laravel 9. i will give you simple example of yajra datatables server side with ajax in laravel 9. In this example of how to use bootstrap datatables server side in laravel 9.

Yajra DataTables is a jQuery plug-in which is helps us in Laravel for more functionalities like searching, sorting, pagination on tables. If you search how to use yajra data tables in laravel then here in this example is perfect implement from scratch.

You have to just follow a few steps for implement data tables in your laravel application. let's start to use data tables in laravel application.

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: Install Yajra Datatable

In this step we need to install yajra datatable via the Composer package manager, so one your terminal and fire bellow command:

composer require yajra/laravel-datatables-oracle
Step 3: Add Dummy Users

In this step, we will create some dummy users using tinker factory. so let's create dummy records using bellow command:

php artisan tinker
Step 4: Create Controller

In this point, now we should create new controller as UserController. this controller will manage layout and getting data request and return response, so put bellow content in controller file:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use DataTables;

class UserController extends Controller
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function index(Request $request)
        if ($request->ajax()) {
            $data = User::select('*');
            return Datatables::of($data)
                    ->addColumn('action', function($row){
                            $btn = '<a href="javascript:void(0)" class="edit btn btn-success btn-sm text-center">View</a>';
                            return $btn;
        return view('users');
Step 5: Create Route

In this is step we need to create route for datatables layout file and another one for getting data. so open your "routes/web.php" file and add following route.

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
Route::get('users', [UserController::class, 'index'])->name('users.index');
Step 6: Create Blade File

In Last step, let's create users.blade.php(resources/views/users.blade.php) for layout and we will write design code here and put following code:

<!DOCTYPE html>
    <title>How to Implement Yajra Datatables Server Side in Laravel 9? -</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link href="" rel="stylesheet">
    <link href="" rel="stylesheet">
    <script src=""></script>  
    <script src=""></script>
    <script src=""></script>
<div class="container">
    <h3 class="text-center mt-3 mb-4">How to Implement Yajra Datatables Server Side in Laravel 9? -</h3>
    <table class="table table-bordered data-table">
                <th width="100px" class="text-center">Action</th>
<script type="text/javascript">
  $(function () {
    var table = $('.data-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('users.index') }}",
        columns: [
            {data: 'id', name: 'id'},
            {data: 'name', name: 'name'},
            {data: 'email', name: 'email'},
            {data: 'action', name: 'action', orderable: false, searchable: false},
Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:


I hope it can help you...

#Laravel 9