Laravel-datatables: Search from combined column didnt work

Created on 30 May 2017  ·  18Comments  ·  Source: yajra/laravel-datatables

Summary of problem or feature request


I had use filtercolumn() to make me can search multiple column like name (combined from first_name and last_name)
i had read your demo but the query is different so i still confused :(

Code snippet of problem

$(function () {
        $("#datatable").DataTable({
            processing: true,
            serverSide: true,
            "order": [[ 1, "asc" ]],
            ajax: "{!! route('admin.customers.getdata','Umum') !!}",
            columns: [
                { data: null, bSortable: false, className: "text-center" },
                { data: "id" },
                { data: "name", name: "name" },
                { data: "type" },
                { data: "email" },
                { data: "location" },
                { data: "active", bSortable: false, className: "text-center" },
                { data: "free_ongkir", bSortable: false, className: "text-center" },
                { data: "action", bSortable: false, className: "text-center" }
            ],
            fnRowCallback: function(nRow, aData, iDisplayIndex){
                $("td:first", nRow).html(iDisplayIndex +1);
                return nRow;
            }
        });

and this the code in my controller

public function getdata($type)
    {
        $users = User::where(['type'=>$type])->whereHas('roles', function ($query) {
            $query->where('slug', '=', 'user');
        })->get();
        foreach ($users as $key => $value) {
            if($value->id == Sentinel::getUser()->id) $value->disabled = "disabled";
            else$value->disabled = "";
        }
        return Datatables::of($users)
        ->editColumn('name', '
            <a href="{{ route(\'admin.customers.show\', $id) }}">{!! $first_name." ".$last_name !!}</a>
        ')
        ->filterColumn('name', function($query, $keyword) {
                    $sql = "CONCAT(users.first_name,'-',users.last_name)  like ?";
                    $query->whereRaw($sql, ["%{$keyword}%"]);
                })
        ->editColumn('type', function($users) {
            if($users->hasAccess('superadmin')) return 'Super Admin';
            else return "Komoditi $users->type";
        })
        ->editColumn('active', function($users) {
            if(!$users->is_approved) {
                return '<span class="has-error form-group"><label><i class="fa fa-times-circle-o"></i> Belum di approve</label></span>';
            } else if(Activation::completed($users)) {
                return '<span class="has-success form-group"><label><i class="fa fa-check"></i> Aktif</label></span>';
            } else {
                return '<span class="has-error form-group"><label><i class="fa fa-times-circle-o"></i> Belum Aktivasi</label></span>';
            }
        })
        ->editColumn('free_ongkir', function($users) {
          if ($users->free_ongkir == 1) {
            return '<span class="has-success form-group"><label><i class="fa fa-check"></i></label></span>';
          }else {
            return '<span class="has-error form-group"><label><i class="fa fa-times-circle-o"></i></label></span>';
          }
        })
        ->editColumn('location', function($users){
            if(empty($users->city->name)) return 'None';
            else return $users->city->name;
        })
        ->addColumn('action', '@if(Sentinel::getUser()->hasAccess(["customers.edit"]))<button class="btn btn-primary" type="button" onClick="location.href=\'{{ route(\'admin.customers.edit\', $id) }}\'"><i class="fa fa-edit"></i></button>
            @endif
            {!! Form::open([
                \'method\'=>\'DELETE\',
                \'route\' => [\'admin.customers.destroy\', $id],
                \'style\' => \'display:inline\'
            ]) !!}
            @if(Sentinel::getUser()->hasAccess(["customers.destroy"]))
                {!! Form::button(\'<i class="fa fa-trash"></i>\', [\'class\' => \'btn btn-danger btn-md\',\'type\'=>\'submit\',\'onclick\'=>\'return confirm("Are you sure want to delete?")\', $disabled]) !!}
             @endif
            {!! Form::close() !!}

        ')
        ->make(true);
    }

System details

  • Operating System : Ubuntu 16.04
  • PHP Version : 7.0
  • Laravel Version : 5.1
  • Laravel-Datatables Version : 6.0
question

Most helpful comment

finally solved, i modify the query in controller and the datatable, like this:

$("#datatable").DataTable({
            processing: true,
            serverSide: true,
            "order": [[ 1, "asc" ]],
            ajax: "{!! route('admin.customers.getdata','Umum') !!}",
            columns: [
                { data: null, bSortable: false, className: "text-center" },
                { data: "id" },
                { data: "name", name: "full_name" },
                { data: "type" },
                { data: "email" },
                { data: "location" },
                { data: "active", bSortable: false, className: "text-center" },
                { data: "free_ongkir", bSortable: false, className: "text-center" },
                { data: "action", bSortable: false, className: "text-center" }
            ],
            fnRowCallback: function(nRow, aData, iDisplayIndex){
                $("td:first", nRow).html(iDisplayIndex +1);
                return nRow;
            }
        });

The controller:

{
        $users = User::select('id','email','first_name','last_name','city_id', 'is_approved','type', DB::raw("CONCAT(first_name,' ',last_name) as full_name"))->where(['type'=>$type])->whereHas('roles', function ($query) {
            $query->where('slug', '=', 'user');
        })->get();
        foreach ($users as $key => $value) {
            if($value->id == Sentinel::getUser()->id) $value->disabled = "disabled";
            else$value->disabled = "";
        }
        return Datatables::of($users)
        ->editColumn('name', '
            <a href="{{ route(\'admin.customers.show\', $id) }}">{!! $first_name." ".$last_name !!}</a>
        ')
        ->editColumn('type', function($users) {
            if($users->hasAccess('superadmin')) return 'Super Admin';
            else return "Komoditi $users->type";
        })
        ->editColumn('active', function($users) {
            if(!$users->is_approved) {
                return '<span class="has-error form-group"><label><i class="fa fa-times-circle-o"></i> Belum di approve</label></span>';
            } else if(Activation::completed($users)) {
                return '<span class="has-success form-group"><label><i class="fa fa-check"></i> Aktif</label></span>';
            } else {
                return '<span class="has-error form-group"><label><i class="fa fa-times-circle-o"></i> Belum Aktivasi</label></span>';
            }
        })
        ->editColumn('free_ongkir', function($users) {
          if ($users->free_ongkir == 1) {
            return '<span class="has-success form-group"><label><i class="fa fa-check"></i></label></span>';
          }else {
            return '<span class="has-error form-group"><label><i class="fa fa-times-circle-o"></i></label></span>';
          }
        })
        ->editColumn('location', function($users){
            if(empty($users->city->name)) return 'None';
            else return $users->city->name;
        })
        ->addColumn('action', '@if(Sentinel::getUser()->hasAccess(["customers.edit"]))<button class="btn btn-primary" type="button" onClick="location.href=\'{{ route(\'admin.customers.edit\', $id) }}\'"><i class="fa fa-edit"></i></button>
            @endif
            {!! Form::open([
                \'method\'=>\'DELETE\',
                \'route\' => [\'admin.customers.destroy\', $id],
                \'style\' => \'display:inline\'
            ]) !!}
            @if(Sentinel::getUser()->hasAccess(["customers.destroy"]))
                {!! Form::button(\'<i class="fa fa-trash"></i>\', [\'class\' => \'btn btn-danger btn-md\',\'type\'=>\'submit\',\'onclick\'=>\'return confirm("Are you sure want to delete?")\', $disabled]) !!}
             @endif
            {!! Form::close() !!}

        ')
        ->make(true);
    }

Thankyou :)

All 18 comments

@faisalhilmi see the solution offered on https://github.com/yajra/laravel-datatables/issues/1173. Thanks!

i see the references then i try modify like this:

{ data: "name", name: "name" },
{ data: "first_name", name: "first_name", visible: false },
{ data: "last_name", name: "last_name", visible: false },

in controller:

        ->editColumn('name', '
            <a href="{{ route(\'admin.customers.show\', $id) }}">{!! $first_name." ".$last_name !!}</a>
        ')
        ->filterColumn('name', function($query, $keyword) {
                $query->orWhere('first_name', 'like', '%'. $keyword . '%');
                $query->orWhere('last_name', 'like', '%'. $keyword . '%');
            })

but the search can only with first_name or last name, not both.
is it possible?

Hmm, I think there is a bug here in relation to multiple term search feature. Are you searching the full name space separated right? I think the keyword passed to filterColumn are now separated into two parts (first & last name).

Can you please try:

->filterColumn('name', function($query, $keyword) {
    $sql = "CONCAT(users.first_name,'-',users.last_name)  like ?";
    $keyword = request('search.value');
    $query->whereRaw($sql, ["%{$keyword}%"]);
})

yes i want search the full name :)
i try like this

$users = User::where(['type'=>$type])->whereHas('roles', function ($query) {
            $query->where('slug', '=', 'user');
        })->get();
        foreach ($users as $key => $value) {
            if($value->id == Sentinel::getUser()->id) $value->disabled = "disabled";
            else$value->disabled = "";
        }
        return Datatables::of($users)
        ->editColumn('name', '
            <a href="{{ route(\'admin.customers.show\', $id) }}">{!! $first_name." ".$last_name !!}</a>
        ')
        ->filterColumn('name', function($query, $keyword) {
    $sql = "CONCAT(users.first_name,'-',users.last_name)  like ?";
    $keyword = request('search.value');
    $query->whereRaw($sql, ["%{$keyword}%"]);
})

but still not work, even i try to change $query on filtercolumn to $users :(

is it something wrong in $users query?

Is there any error return on your ajax response?

no error, just "No matching records found" when i search "customer umum"
"customer" is first_name on users table
"umum" is last_name on users table

Can you try inspecting the ajax request and paste the generated sql? I suspect that there is something wrong on your sql. Try running it manually.

Whoops just realized that you are using v6.x and multiple term search is not applicable on it. Sorry!

"yajra/laravel-datatables-oracle": "~6.0", thats the problem?

No it's not the problem, I was just referencing a wrong feature. I think the error here is on your generated SQL.

You use concat on first and last name but use - to separate it not space. Maybe that's causing the issue?

no its still cant find fullname :(

You need to debug the generated query. Use chrome dev tools or firebug and inspect the generated SQL.

am i must add concat in this?

$users = User::where(['type'=>$type])->whereHas('roles', function ($query) {
            $query->where('slug', '=', 'user');
        })->get();

Oh, you need to remove ->get(); and let the package do the pagination on the sql level.

i cant because it siblings to another process :(

If you use ->get(), you are now using collection instead of eloquent. And I think filterColumn is not applicable on Collection. You need to find a way to remove get on your query. That's the issue why search is failing.

finally solved, i modify the query in controller and the datatable, like this:

$("#datatable").DataTable({
            processing: true,
            serverSide: true,
            "order": [[ 1, "asc" ]],
            ajax: "{!! route('admin.customers.getdata','Umum') !!}",
            columns: [
                { data: null, bSortable: false, className: "text-center" },
                { data: "id" },
                { data: "name", name: "full_name" },
                { data: "type" },
                { data: "email" },
                { data: "location" },
                { data: "active", bSortable: false, className: "text-center" },
                { data: "free_ongkir", bSortable: false, className: "text-center" },
                { data: "action", bSortable: false, className: "text-center" }
            ],
            fnRowCallback: function(nRow, aData, iDisplayIndex){
                $("td:first", nRow).html(iDisplayIndex +1);
                return nRow;
            }
        });

The controller:

{
        $users = User::select('id','email','first_name','last_name','city_id', 'is_approved','type', DB::raw("CONCAT(first_name,' ',last_name) as full_name"))->where(['type'=>$type])->whereHas('roles', function ($query) {
            $query->where('slug', '=', 'user');
        })->get();
        foreach ($users as $key => $value) {
            if($value->id == Sentinel::getUser()->id) $value->disabled = "disabled";
            else$value->disabled = "";
        }
        return Datatables::of($users)
        ->editColumn('name', '
            <a href="{{ route(\'admin.customers.show\', $id) }}">{!! $first_name." ".$last_name !!}</a>
        ')
        ->editColumn('type', function($users) {
            if($users->hasAccess('superadmin')) return 'Super Admin';
            else return "Komoditi $users->type";
        })
        ->editColumn('active', function($users) {
            if(!$users->is_approved) {
                return '<span class="has-error form-group"><label><i class="fa fa-times-circle-o"></i> Belum di approve</label></span>';
            } else if(Activation::completed($users)) {
                return '<span class="has-success form-group"><label><i class="fa fa-check"></i> Aktif</label></span>';
            } else {
                return '<span class="has-error form-group"><label><i class="fa fa-times-circle-o"></i> Belum Aktivasi</label></span>';
            }
        })
        ->editColumn('free_ongkir', function($users) {
          if ($users->free_ongkir == 1) {
            return '<span class="has-success form-group"><label><i class="fa fa-check"></i></label></span>';
          }else {
            return '<span class="has-error form-group"><label><i class="fa fa-times-circle-o"></i></label></span>';
          }
        })
        ->editColumn('location', function($users){
            if(empty($users->city->name)) return 'None';
            else return $users->city->name;
        })
        ->addColumn('action', '@if(Sentinel::getUser()->hasAccess(["customers.edit"]))<button class="btn btn-primary" type="button" onClick="location.href=\'{{ route(\'admin.customers.edit\', $id) }}\'"><i class="fa fa-edit"></i></button>
            @endif
            {!! Form::open([
                \'method\'=>\'DELETE\',
                \'route\' => [\'admin.customers.destroy\', $id],
                \'style\' => \'display:inline\'
            ]) !!}
            @if(Sentinel::getUser()->hasAccess(["customers.destroy"]))
                {!! Form::button(\'<i class="fa fa-trash"></i>\', [\'class\' => \'btn btn-danger btn-md\',\'type\'=>\'submit\',\'onclick\'=>\'return confirm("Are you sure want to delete?")\', $disabled]) !!}
             @endif
            {!! Form::close() !!}

        ')
        ->make(true);
    }

Thankyou :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SGarridoDev picture SGarridoDev  ·  3Comments

Mopster picture Mopster  ·  3Comments

macnux picture macnux  ·  3Comments

hari-web picture hari-web  ·  3Comments

sangnguyenplus picture sangnguyenplus  ·  3Comments