phplaravellaravel-5concatenationpluck

Laravel use of concat with pluck method


I am using Laravel.5.3 and below is my query

$ProjectManagers = Employees::where("designation" , 1)
->pluck(DB::raw('CONCAT(first_name," ",last_name) AS name'),'id');

which throws an error that

Illegal offset type in isset or empty

May i know if this is the correct method ?

if i dont use contact and use like

$ProjectManagers = Employees::where("designation" , 1)->pluck('first_name','id');

which is working correct and giving me result

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [8] => Punit
        )

)

Expected Result :

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [8] => Punit Gajjar
        )

)

where first name and last name are concatenated.


Solution

  • Try changing the eloquent query to:

    $ProjectManagers = Employees::select(
                DB::raw("CONCAT(first_name,' ',last_name) AS name"), 'id')
                ->where('designation', 1)
                ->pluck('name', 'id');