Laravel-datatables: How to filter a concatenated result value in multi fields in Laravel 5.3

Created on 3 Feb 2017  ·  3Comments  ·  Source: yajra/laravel-datatables

Hi,

In my datatable, I'm retrieving the name field as CONCAT(fname," ", "lname"). I have to search name field in the datatable and it should return matching with either fname or lname. Please let me know how to do this:

js code:

columns  : [
             { data: 'name', name: 'fname' }, //I have tried this also : ['fname','lname' ]
                --------------------

PHP code:

$users = User::leftJoin('address', 'address.user_id', '=', 'users.user_id') 
                    ->select([\DB::Raw(
                        'CONCAT(fname," ",lname) as name, CONCAT(country_code, "", mobile_number) as mobile_num, IF(status = 1, "Active", "Inactive") as status,
                        (SELECT count(subscription_id) FROM subscription WHERE subscription.user_id = users.user_id AND subscription.status IN (2,4)) as totalActivesubscription'

                        ), 'gender', 'city as address', 'city','users.user_id', 'email_id'])

                    ->where('users.role_id', '=', $aParam['role_id']);

            return Datatables::of($users)
                ->addColumn('actions', function ($users) {
                    return view('users.templates.user_action', [
                        'user' => $users
                        ])->render();
                })


            ->make(true);

System details:"

  • Operating System : Windows8, Linux
  • PHP Version : 5.6
  • Laravel Version : 5.3
  • Laravel-Datatables Version : latest
question

Most helpful comment

Maybe try using this trick on your js:

columns  : [
   { data: 'name', name: 'fname' },
   { data: 'name', name: 'lname', visible: false },
...

Or use filterColumn api.

All 3 comments

Maybe try using this trick on your js:

columns  : [
   { data: 'name', name: 'fname' },
   { data: 'name', name: 'lname', visible: false },
...

Or use filterColumn api.

by using filterColumn api it worked. Thank you.
But what if we need to search a field of another table (which joined in the main query).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kamrava picture kamrava  ·  3Comments

josiahke picture josiahke  ·  3Comments

hohuuhau picture hohuuhau  ·  3Comments

shadoWalker89 picture shadoWalker89  ·  3Comments

techguydev picture techguydev  ·  3Comments