3

The Yajra Datatable global search is not working.

Controller Code

public function getTable() {
        $query = Customer::query();
        return Datatables::of($query)
            ->addColumn('name', function ($query) {
                return "$query->name";
            })
            ->addColumn('number', function ($query) {
                return "$query->number";
            })
            ->addColumn('category', function ($query) {
                return "$query->category";
            })
            ->addColumn('customer_fields', function ($query) {
                return "asd";
            })
            ->addColumn('address', function ($query) {
                return "".customerAddressFormatHelper($query -> address, $query -> location_name)."";
            })
            ->addColumn('created_at', function ($query) {
                return "$query->created_at";
            })
            ->addColumn('action', function ($query) {
                return "<a href='".route('customers.show', $query -> id)."' class='btn btn-primary'><i class='fa fa-eye'></i> </a>";
            })
            ->escapeColumns(['action'])
            ->make(true);
    }

The JS Code

$(document).ready(function() {
            $('#customer_table').DataTable( {
                dom: 'Bfrtip',
                buttons: [
                    'print',
                    'copyHtml5',
                    'excelHtml5',
                    'csvHtml5',
                    'pdfHtml5'
                ],
                processing: true,
                serverSide: true,
                ajax: '{{ route('customers.table') }}',
                columns: [
                    {data: 'name',              name: 'name'},
                    {data: 'number',            name: 'number'},
                    {data: 'category',          name: 'category'},
                    {data: 'customer_fields',   name: 'customer_fields'},
                    {data: 'address',           name: 'address'},
                    {data: 'created_at',        name: 'created_at'},
                    {data: 'action',            name: 'action'},
                ]
            } );
        } );

Everything works correctly without errors or anything but the problem is that Global Search always return the same amount of results in the network tab.

What I have tried:

I have tried to replace the $query = Customer::query(); code with $query = Customer::all(); that works perfectly and the search works perfectly..

Problem is after I added the all() method it became very very slow because I am using collections not query builder.

DOEE
  • 73
  • 2
  • 8

1 Answers1

0

You can try this:

public function getTable() {
        $query = Customer::query();
        return Datatables::of($query)
            ->addColumn('customer_fields', function ($query) {
                return "asd";
            })
            ->addColumn('address', function ($query) {
                return "".customerAddressFormatHelper($query -> address, $query -> location_name)."";
            })
            ->addColumn('action', function ($query) {
                return "<a href='".route('customers.show', $query -> id)."' class='btn btn-primary'><i class='fa fa-eye'></i> </a>";
            })
            ->escapeColumns(['action'])
            ->make(true);
    }

and let me know if that worked.

Mahdi Jafari
  • 347
  • 6
  • 22