Search datatable with relationship values

  datatable, jquery, laravel, search, server-side

I’m having a problem when I need to make a Search on my datatable. I have 4 columns with relation with another tables. On my controller I make a join to convert this data, so when I put some ID on search box, the filter works nicely, but I need make this search work only with the data who is presented on screen.

Here is my controller:

public function revList(Request $request)
{
    if(auth()->user()->can('Visualizar Revisão')){
        $revisoes = DB::table('revisao_ambientes')
            ->join('unidades', 'revisao_ambientes.unidade_id', '=', 'unidades.id')
            ->join('blocos', 'revisao_ambientes.bloco_id', '=', 'blocos.id')
            ->join('ambientes', 'revisao_ambientes.ambiente_id', '=', 'ambientes.id')
            ->join('users', 'revisao_ambientes.user_id', '=', 'users.id')
            ->select([
                'revisao_ambientes.unidade_id', 'unidades.name as unidade_name', 
                'revisao_ambientes.bloco_id', 'blocos.name as bloco_name', 
                DB::raw('CONCAT(ambientes.sala, " - ", ambientes.name) as ambiente_name'),
                'revisao_ambientes.user_id', 'users.name as user_name', 
                'revisao_ambientes.created_at as created_at',
                'revisao_ambientes.rev_id as rev_id',
            ]);                 

        return datatables()->of($revisoes) 
            ->addColumn('detalhes', function($row) {
                return '<a href="revisao/'. $row->rev_id .'" class="label label-info">Detalhes</a>';
            })
            //Try to make a filter by the building (Don't work)
            ->filterColumn('bloco_name', function($revisoes, $search) {
                $revisoes->where('blocos', 'LIKE', "{$search}%");
            })
            ->rawColumns(['detalhes'])
            ->make(true);
        }else{
            return view('errors.401');
        } 
    }

And here is my script of server side datatable:

$(document).ready( function () {
$('#rev-datatable').DataTable({                 
  dom: 'Blfrtip',
      lengthMenu: [
          [10, 25, 50, 100, -1], 
          [10, 25, 50, 100, "Todos"]
        ],
      buttons: [
          { extend: 'copy', text: 'Copiar', exportOptions: {
                columns: [ 0, 1, 2, 3, 4 ]
            }},
          { extend: 'excel', text: 'Excel', exportOptions: {
                columns: [ 0, 1, 2, 3, 4]
            }},
          { extend: 'pdf', text: 'PDF', orientation: 'landscape',
            pageSize: 'LEGAL', exportOptions: {
                columns: [ 0, 1, 2, 3, 4]
            }},             
      ],
      processing: true,
      Filter: true,
      lengthChange: true,
      lengthMenu: [[10, 25, 50, 100, 200, -1], [10, 25, 50, 100, 200, "Todos"]],
      order:[[0,'desc']],
      serverSide: true,
      ajax: "{{ url('/revisao/rev-list') }}",
      columns: [
          { data: 'created_at', name: 'created_at' },
          { data: 'user_name', name: 'user_id' },
          { data: 'unidade_name', name: 'unidade_id' },
          { data: 'bloco_name', name: 'bloco_id' },
          { data: 'ambiente_name', name: 'ambiente_id' }, 
          { data: 'detalhes', searchable: false, orderable: false},                 
             ] , 
    });
 });

What I need to do to make this works?

thanks advance!

Source: Laravel StackOverflow

Leave a Reply