Laravel pluck but combining first name + last name for select
Asked Answered
P

8

5

Using select2 with a Laravel / Vue project and need to return JSON in the following format:

[
    { id: 0, text: 'enhancement' },
    { id: 1, text: 'bug' }
]

In Laravel I know I can use pluck to create my list data e.g. for customers:

$customers = Customer::pluck('id', 'first_name');

But Want to return the id and first name + last name as a single name.

How can I do this?

Partan answered 10/6, 2017 at 16:22 Comment(0)
S
28

Have you tried using Accessors?

https://laravel.com/docs/5.4/eloquent-mutators#defining-an-accessor

I have not tested it but this could work:

add this to your Customer Eloquent Model:

    public function getFullNameAttribute()
    {
       return ucfirst($this->first_name) . ' ' . ucfirst($this->last_name);
    }

and then try:

UPDATED pluck on accessor will only work on a collection. If you try Customer::pluck('id', 'full_name') it will not work since there is no db column named full_name, therefore you must use Customer::all()->pluck('full_name', 'id')

$customers = Customer::all()->pluck('full_name', 'id');
  • as a side note, for performance it is probably better to do Customer::all(['id', 'first_name', 'last_name'])->pluck(...) so that we don't pull unnecessary columns from the db.

Hope this helps.

Updated Date:- 26th Aug, 2021 If we use computed Attribute accessor functionality, then mind it one important thing...

Laravel Accessor functionality works after the Data fetched from DataBase. So we have to declare "pluck(accessorName)" at the end of Query....

For Example:-

Wrong Methods:-

$data = Model::pluck('full_name','id)->get(); 
$data = Model::pluck('full_name','id)->all();

in above two queries if you does not have full_name field in DataTable you will get Unknown column error

Right Methods:-

$data = Model::get()->pluck('full_name','id');
$data = Model::all()->pluck('full_name','id');

in above two queries it will works perfectly even if you doesn't have full_name field in DataTable

Stiver answered 10/6, 2017 at 16:27 Comment(1)
thank you for the explanation that this would only work on a collection, the very reason why it is not working on me. upvoted!Jule
H
3

You can do it like this,

$customers = DB::table('customers')->select("id", "CONCAT(firstname,' ',lastname) as fullname")->get();

or you also do like this,

$customers = DB::table('customers')->select(DB::raw('CONCAT(firstname,' ',lastname) as fullname, id'))->get();

or with PHP way,

$fullname = $customers->firstname. " " .$customers->lastname;
Hoenir answered 10/6, 2017 at 16:41 Comment(1)
it gives me SQLSTATE[42S22]: Column not found: 1054 Unknown column 'CONCAT...Gulledge
S
3

Set this in the User model

public function getFullNameAttribute()
{
    return $this->first_name . ' ' . $this->last_name;
}

Then make sure you add this too

protected $appends = ['full_name'];
Sportscast answered 12/8, 2022 at 18:37 Comment(0)
A
1

Use this code. Hope it will work. And I solve this problem using this code

User::select(DB::raw("CONCAT(first_name, ' ', last_name) AS full_name"),"id")->pluck("full_name","id");
Aerodynamics answered 10/6, 2017 at 16:22 Comment(0)
A
1

For me it worked

\DB::table("admin as c")->select(\DB::raw("CONCAT(FirstName, ' ', LastName) AS FIRSTNAME"),"c.AdminID")->pluck("FIRSTNAME","AdminID");
Aleen answered 2/7, 2019 at 8:18 Comment(0)
A
0

User Model :

public function getFullNameAttribute()
{
    return "{$this->first_name} {$this->last_name}";
}

Get query will result in collection :

$agents = User::whereId($agent->id)->get()->pluck('full_name', 'id');

Inorder to convert variable from objects to array :

  $agents = json_decode(json_encode($agents), true);

It worked for me.Enjoy.

Adalia answered 24/5, 2020 at 9:34 Comment(0)
M
0
   /**
 * Get the full name of the user.
 *
 * @return string
 */
public function getFullNameAttribute()
{
    return "{$this->first_name} {$this->last_name}";
}

and use this pluck

User::all()->sortBy('id')->pluck('full_name', 'id')

like this

   public static function list()
{
    return Cache::tags('usersCustomers')->rememberForever(md5('usersCustomers.list:' . locale()), function () {
        return self::all()->sortBy('id')->pluck('full_name', 'id');
    });
}
Murrain answered 3/3, 2021 at 12:15 Comment(0)
A
0

** Use this code. Hope it will work. And I solve this problem using this code**

       User::select(DB::raw("CONCAT(first_name, ' ', last_name) AS 
       full_name"),"id")->pluck("full_name","id");
Aerodynamics answered 23/6, 2021 at 21:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.