Laravel 10 Yajra Datatables Tutorial Example

In Laravel 10, Yajra Datatables is a popular Laravel package that provides an easy way to work with DataTables, a powerful jQuery plugin for creating interactive and feature-rich tables in web applications.

Server-side processing: DataTables has built-in support for server-side processing, which means that it can handle large datasets efficiently by retrieving data from the server as needed. Yajra Datatables provides a convenient way to implement server-side processing in Laravel, allowing you to handle and manipulate large datasets without performance issues

Step 1: Install Laravel App

composer create-project laravel/laravel example-YajraTable

Step 2: Install Yajra Datatable

composer require yajra/laravel-datatables-oracle

Step 3: Add Dummy Users

Run Command: 

php artisan tinker
  User::factory()->count(20)->create()

Step 4: Create Controller

app/Http/Controllers/UserController.php 

<?php
   
    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)
                        ->addIndexColumn()
                        ->addColumn('action', function($row){
           
                                $btn = '<a href="javascript:void(0)" class="edit btn btn-primary btn-sm">View</a>';
          
                                return $btn;
                        })
                        ->rawColumns(['action'])
                        ->make(true);
            }
              
            return view('users');
        }
    }

Step 5: Add Route

routes/web.php 

<?php
  
    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

resources/views/users.blade.php 

<!DOCTYPE html>
    <html>
    <head>
        <title>Laravel 10 Yajra Datatables Tutorial - webthestuff.com</title>
        <meta name="csrf-token" content="{{ csrf_token() }}">
        <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet">
        <link href="https://cdn.datatables.net/1.11.4/css/dataTables.bootstrap5.min.css" rel="stylesheet">
        <script src="https://code.jquery.com/jquery-3.5.1.js"></script>  
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.js"></script>
        <script src="https://cdn.datatables.net/1.11.4/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
        <script src="https://cdn.datatables.net/1.11.4/js/dataTables.bootstrap5.min.js"></script>
    </head>
    <body>
         
    <div class="container">
        <h1>Laravel 10 Yajra Datatables Tutorial - webthestuff.com</h1>
        <table class="table table-bordered data-table">
            <thead>
                <tr>
                    <th>No</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th width="100px">Action</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
         
    </body>
         
    <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},
            ]
        });
          
      });
    </script>
    </html>

Run Laravel App:

php artisan serve

Now, Go to web browser, type the given URL and see the output:

http://localhost:8000/users