Laravel 9 Yajra Datatables Example Tutorial Server Side Ajax

Throughout this Laravel 9 yajra datatables Example Tutorial; you will learn how to create and show the yajra datatables laravel 9 example for server side ajax.

This tutorial gives you step by step guide to implement the yajra datatables laravel examples.. You can use the yajra datatabales with dyamic database records to show with pagination and search box with bootstrap.

You can use the this server side Yajra datatable example with search, pagination in your larfavel 5, laravel 6, laravel 7, laravel 8 or laravel 9 sequencelly.

Laravel 9 Datatables Example

DataTables is a plug-in powered by jQuery often known as Javascript library. It is a notably flexible tool, developed upon the foundations of progressive and dynamic enhancement, that incorporates all of these subtle and advanced features to any static HTML table.

Yajra datatables top-notch features:

  • Pagination
  • Instant search
  • Multi-column ordering
  • Use almost any data source
  • Easily theme-able
  • Wide variety of extensions
  • Mobile friendly
  • Fully internationalisable

Follow the below steps to implement Yajra datatables in laravel application:

  • Step 1: Install Laravel App
  • Step 2: Install Yajra Datatables
  • Step 3: Create Model and Migrations
  • Step 4: Create Controller
  • Step 5: Add Route
  • Step 6: Display Data in Datatables Blade File
  • Step 7: Generate Fake Records for Test
  • Step 8: Start Laravel App

Install Laravel App

First we need to install new laravel application using the below-mentioned artisan command.

composer create-project laravel/laravel laravel-yajra-datatables

Get into the project:

cd laravel-yajra-datatables 

Install Yajra Datatable Package

Install the Yajra DataTable plugin in Laravel using the following command:

composer require yajra/laravel-datatables-oracle

Additionally, enlarge the foundational service of the package such as datatable service provider in providers and alias inside the config/app.php file.

'providers' => [
'aliases' => [
	'DataTables' => Yajra\DataTables\Facades\DataTables::class,

Run vendor publish command further this step is optional:

php artisan vendor:publish --provider="Yajra\DataTables\DataTablesServiceProvider"

Create Model and Migrations

Run command to create a model, and it holds the schema of the database table.

php artisan make:model Student -m

Now open the database migration file and put the below code on it.



use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateStudentsTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('students', function (Blueprint $table) {

     * Reverse the migrations.
     * @return void
    public function down()

Open app/Models/Student.php and add the fillable properties.


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
    use HasFactory;

    protected $fillable = [

Run migration using the following command.

php artisan migrate

Create Controller and Update Code

Create a controller using the below command.

php artisan make:controller StudentController

Open app/Http/Controllers/StudentController.php file and add the following code.


namespace App\Http\Controllers;

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

class StudentController extends Controller
    public function index(Request $request)
        if ($request->ajax()) {
            $data = Student::latest()->get();
            return Datatables::of($data)
                ->addColumn('action', function($row){
                    $actionBtn = '<a href="javascript:void(0)" class="edit btn btn-success btn-sm">Edit</a> <a href="javascript:void(0)" class="delete btn btn-danger btn-sm">Delete</a>';
                    return $actionBtn;

        return view('students');


Add Route

Open routes/web.php and add the given code.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\StudentController;

Route::get('students', [StudentController::class, 'index'])->name('students');

Display Data with Yajra Datatables

Now we will create an students.blade.php file spontaneously. Designing a beautiful table is a bit tricky if you don’t know how to work with HTML and CSS. This is what bootstrap set out to do.

Open resources/views/students.blade.php file and place the following code.

<!DOCTYPE html>
    <title>Laravel 8|9 Datatables Tutorial</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="stylesheet" href=""/>
    <link href="" rel="stylesheet">
    <link href="" rel="stylesheet">

<div class="container mt-5">
    <h2 class="mb-4">Laravel 8|9 Yajra Datatables Example</h2>
    <table class="table table-bordered yajra-datatable">

<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script type="text/javascript">
  $(function () {

    var table = $('.yajra-datatable').DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('students') }}",
        columns: [
            {data: 'DT_RowIndex', name: 'DT_RowIndex'},
            {data: 'name', name: 'name'},
            {data: 'email', name: 'email'},
            {data: 'username', name: 'username'},
            {data: 'phone', name: 'phone'},
            {data: 'dob', name: 'dob'},
                data: 'action',
                name: 'action',
                orderable: true,
                searchable: true


Create Dummy Records for Testing

To give you the demo of Yajra DataTables in Laravel, we need to generate a handful of dummy data. Use the built-in plugin Faker, and it respectively creates the fake data in the database.

Create Student Seeder using the following command:

php artisan make:seeder StudentSeeder

Open database\seeders\StudentSeeder.php and put the below code on it.


namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Faker\Factory as Faker;
use App\Models\Student;

class StudentSeeder extends Seeder
     * Run the database seeds.
     * @return void
    public function run()
        $faker = Faker::create();
        $gender = $faker->randomElement(['male', 'female']);
    	foreach (range(1,200) as $index) {
                'email' => $faker->email,
                'name' => $faker->name($gender),
                'username' => $faker->username,
                'phone' => $faker->phoneNumber,
                'dob' => $faker->date($format = 'Y-m-d', $max = 'now')

Now run the below command for generating test records for students table:

php artisan db:seed --class=StudentSeeder

Start Laravel App

Now run the following command and check our progress on the browser.

php artisan serve

Here is the output you get after executing the mentioned command:

I hope you enjoy with how to make Yajra datatables in Laravel 8|9 tutorial exampl. Feel free to ask us using comment section and follow us for more updates.

Leave a Comment