Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I add filtering options to Yajra DataTables in Laravel 11
    Laravel 11 Yajra DataTables DataTables filtering Laravel DataTables tutorial dynamic search Laravel DataTables search functionality


How do I add filtering options to Yajra DataTables in Laravel 11


To add filtering options to Yajra DataTables in Laravel 11, you can follow these steps:

1. Modify the DataTable Class:
In your DataTable class (e.g., `UsersDataTable`), you can override the `filterColumns()` method to define the columns that should be searchable. Here's an example:

php
protected function filterColumns($query, $keyword)
{
    return $query->where('users.name', 'like', "%{$keyword}%")
                  ->orWhere('users.email', 'like', "%{$keyword}%");
}

In this example, we're adding a search filter for the "Name" and "Email" columns.

2. Update the View:
In your view, you can add the filtering functionality to the DataTable:

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>

@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},
            ],
            initComplete: function () {
                this.api().columns().every(function () {
                    var column = this;
                    var input = document.createElement("input");
                    $(input).appendTo($(column.footer()).empty())
                        .on('change', function () {
                            column.search($(this).val(), false, false, true).draw();
                        });
                });
            }
        });
    });
</script>
@endpush

In this example, we're adding an input field for each column in the table footer. When the user types in the input field, the DataTable will automatically filter the data based on the entered value.

By following these steps, you can add filtering options to Yajra DataTables in your Laravel 11 application. The package provides a flexible API for adding advanced filtering features to your DataTables.

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://yajrabox.com/docs/laravel-datatables/10.0
[5] https://www.binaryboxtuts.com/php-tutorials/laravel-tutorials/laravel-11-crud-using-ajax-with-datatables-tutorial/