Install Yajra Datatables Laravel 11 Example

Hello developers, in this guide, we'll see how to install Yajra datatables laravel 11 example. Here, we'll use yajra/laravel-datatables composer package to use data tables in laravel 11. 

In this tutorial, I'll walk you through the straightforward process of installing Yajra Datatables in your Laravel 11 application.

Yajra datatable provides different kinds of functionalities searching, sorting, pagination, ordering, etc, Also you can search on the server side and client side.

Laravel 11 Yajra Datatable Example



Step 1: Install Laravel 11

First, we'll install the laravel 11 application using the following command.

composer create-project laravel/laravel laravel-11-example


Step 2: Install Yajra Datatables

Then, we'll install yajra/datatable composer package using the following command.

composer require yajra/laravel-datatables


Step 3: Add Dummy Users

Now, we'll create some dummy records using the tinker. So, run the following command.

php artisan tinker


Step 4: Create Controller

In this step, we'll create a UserController.php file. In this file we'll define the function and add the datatable.


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use DataTables;
class UserController extends Controller
     * Display a listing of the resource.
     * Yajra Datatable Laravel 11 - techsolutionstuff
     * @return \Illuminate\Http\Response
    public function index(Request $request)
        if ($request->ajax()) {

            $data = User::query();

            return Datatables::of($data)
                    ->addColumn('action', function($row){
                            $btn = '<a href="javascript:void(0)" data-id="'.$row->id.'" class="edit btn btn-primary btn-sm">View</a>';
                            $btn .= '<a href="javascript:void(0)" data-id="'.$row->id.'" class="delete btn btn-danger btn-sm">Delete</a>';
                            return $btn;
        return view('users');
Step 5: Create Route

Now, define the routes into the web.php file. So, add the following code snippets.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
Route::get('users', [UserController::class, 'index'])->name('users.index');
Step 6: Create Blade File

Then, we'll create a users.blade.php file to display yajra datatable.


<!DOCTYPE html>
    <title>Install Yajra Datatables Laravel 11 Example - Techsolutionstuff</title>
    <link href="" rel="stylesheet">
    <link href="" rel="stylesheet">
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
<div class="container">
    <div class="card mt-5">
        <h3 class="card-header p-3">Install Yajra Datatables Laravel 11 Example - Techsolutionstuff</h3>
        <div class="card-body">
            <table class="table table-bordered data-table">
                        <th width="100px">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},
Step 7: Run the Laravel App

Now, run the laravel 11 application using the following command.

php artisan serve


You might also like:


Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.