Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can I use Yajra DataTables with Laravel 11's built-in pagination


Can I use Yajra DataTables with Laravel 11's built-in pagination


Yes, you can use Yajra DataTables with Laravel 11's built-in pagination. Here's how you can do it:

1. Install the Yajra DataTables Package:
Install the Yajra DataTables package using Composer:


   composer require yajra/laravel-datatables-oracle:"^10.3.1"
   

2. Create a DataTable Class:
Generate a new DataTable class using the following Artisan command:


   php artisan make:datatable UsersDataTable
   

This will create a `UsersDataTable` class in the `app/DataTables` directory.

3. Modify the DataTable Class:
In the `UsersDataTable` class, you can use the `with()` method to pass the pagination parameters to the DataTable:

php
   public function with($query)
   {
       return $query->paginate($this->getLimit());
   }
   

This will use Laravel's built-in pagination instead of the DataTables' own pagination.

4. Update the View:
In your view, you can use the `links()` method to render the pagination links:

html
   <table class="table table-bordered data-table">
       <thead>
           <tr>
               <th>ID</th>
               <th>Name</th>
               <th>Email</th>
               <th>Created At</th>
               <th>Updated At</th>
               <th>Action</th>
           </tr>
       </thead>
       <tbody>
       </tbody>
   </table>

   {!! $dataTable->links() !!}

   @push('scripts')
   <script>
       $(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: 'created_at', name: 'created_at'},
                   {data: 'updated_at', name: 'updated_at'},
                   {data: 'action', name: 'action', orderable: false, searchable: false},
               ]
           });
       });
   </script>
   @endpush
   

In this example, we're using the `{!! $dataTable->links() !!}` syntax to render the pagination links.

By following these steps, you can use Yajra DataTables with Laravel 11's built-in pagination. This allows you to take advantage of the powerful features of both the Yajra DataTables package and Laravel's pagination functionality.

Citations:
[1] https://itcodstuff.com/blog/laravel-yajra-datatables-tutorial/
[2] https://www.youtube.com/watch?v=fQDulbC3Smc
[3] https://www.itsolutionstuff.com/post/laravel-11-yajra-datatables-example-tutorialexample.html
[4] https://www.youtube.com/watch?v=fSN_FNVDqs8
[5] https://www.itsolutionstuff.com/post/how-to-use-yajra-datatables-in-laravel-6example.html