How To Install & Use Yajra Datatables In Laravel 7 Web Applications




In the previous tutorial, you have learned how to submit the form without a whole web page refresh or reload using ajax and jQuery validation in laravel web application (laravel ajax form submit with validation).

In this tutorial, you will learn how to install and use yajra dataTables packages in your web application. This tutorial has main purpose to explain the easy way and step by step to install yajra dataTables package in your web application.

Laravel DataTables Example Tutorial

  • Install Laravel Application
  • Configure Database Detail
  • Run Migration
  • Install and Register Yajra DataTable
  • Generate Fake Data in Table
  • Create Routes
  • Generate Controller By Artisan
  • Create Blade View
  • Start Development Server

Install Laravel Application

First Step, you need to download or install laravel web application new setup on your local machine. you can use the following command to download or install laravel application new setup:

composer create-project laravel/laravel laravel-datatables-app –-prefer-dist

If you want to install a specific version of laravel, you can follow the following tutorial

Configure Database Detail

After that, you will set up the database credentials in .env file. So open your .env file and update database credentials following:


Run Migration

Before you will run php artisan migrate command. Go to app/providers/AppServiceProvider.php and update the below code :

use Illuminate\Support\Facades\Schema;
function boot()

Next, go to command prompt and run the following command:

php artisan migrate


php artisan migrate:fresh

This php artisan migrate command creates a table in your database.

Install and Register Yajra Datatable

Next, you need to install yajra dataTables laravel package in your web applications. So go to your command prompt and run the following command to install the latest laravel yajra dataTables package:

composer require yajra/laravel-datatables-oracle

When the package of yajra datatables is installed. After that this package will have to be registered in the Laravel web application of the app. For this, you can register this package of instruction follow below.


 'providers' => [

 'aliases' => [

  'Datatables' => Yajra\Datatables\Facades\Datatables::class,

When you will register the package of yajra dataTables. After this the package will have to be published with the command given below.

php artisan vendor:publish

Generate Fake data in Table

Next, we need to generate fake data into the MySQL database table by using php artisan command. So go to your command prompt (cli) and then go to your project root directory. After that run the following command:

php artisan tinker

After run the php artisan tinker. Use the following command. This command will add 200 fake data in your MySQL database table users:

>>> factory(App\User::class, 200)->create();

Create Routes

Next step, go to routes/web.php file and update the following routes into your file:

//display users list
Route::get('list','[email protected]');

Generate Controller By Artisan

Now, you need to generate or create a controller file using php artisan command, so use the php artisan make:controller command and generate/create a new controller file:

php artisan make:controller DataTableController

We will create a method in this controller file. This method will also show the list and handle the Ajax request.

After that, go to App/Http/Controllers/ directory and find the created DataTableController file and update the following code into your file:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator,Redirect,Response;
Use App\User;
class DataTableController extends Controller
    public function index() 
        if(request()->ajax()) {
            return datatables()->of(User::select([
                'id','name' , 'email', 'created_at'
        return view('list');

Create Blade Views

Next, you need to create a blade views file, so go resource/views directory and create the following blade views file.

Go to resource/views/ then create a new file name list.blade.php and update the following code:

<!DOCTYPE html>
<html lang="en">

<title>Laravel DataTables Example -</title>

<script src=""></script> 

<link href="" rel="stylesheet">

<link  href="" rel="stylesheet">

<script src=""></script>

         <div class="container mt-4">
            <h2 class="text-center mt-2 mb-3 alert alert-success">Laravel DataTable Example</h2>
            <table class="table table-bordered" id="laravel-datatable">
                     <th>Created at</th>
     $(document).ready( function () {
           processing: true,
           serverSide: true,
           ajax: "{{ url('list') }}",
           columns: [
                    { data: 'id', name: 'id' },
                    { data: 'name', name: 'name' },
                    { data: 'email', name: 'email' },
                    { data: 'created_at', name: 'created_at' }

Start development server

You can use the following command artisan command to start the development server:

php artisan serve

The final step, go to your browser and type the below-given URL into your browser:




You can also download laravel yajra dataTable web application here Download.

The yajra dataTables Web Application Looks Like:

Laravel 6 yajra dataTables


This tutorial idea has taken from this