Laravel 采摘但结合名字+姓氏进行选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44475537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:07:05  来源:igfitidea点击:

Laravel pluck but combining first name + last name for select

laravellaravel-5eloquentjquery-select2

提问by Lovelock

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

将 select2 与 Laravel / Vue 项目一起使用,并且需要以以下格式返回 JSON:

[
    { 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:

在 Laravel 中,我知道我可以使用 pluck 创建我的列表数据,例如为客户:

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

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

但是想要将 id 和名字 + 姓氏作为单个名称返回。

How can I do this?

我怎样才能做到这一点?

回答by dev7

Have you tried using Accessors?

您是否尝试过使用访问器?

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

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:

将此添加到您的 Customer Eloquent 模型中:

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

and then try:

然后尝试:

UPDATEDpluck 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('id', 'full_name')

UPDATEDpluck on accessor 仅适用于集合。如果您尝试Customer::pluck('id', 'full_name')它将不起作用,因为没有名为 full_name 的 db 列,因此您必须使用Customer::all()->pluck('id', 'full_name')

$customers = Customer::all()->pluck('id', 'full_name');
  • 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.
  • 作为旁注,为了提高性能,最好不要Customer::all(['id', 'first_name', 'last_name'])->pluck(...)从数据库中提取不必要的列。

Hope this helps.

希望这可以帮助。

回答by Muhammad Bilal

For me it worked

对我来说它有效

\DB::table("admin as c")->select(\DB::raw("CONCAT(FirstName, ' ', LastName) AS FIRSTNAME"),"c.AdminID")->pluck("FIRSTNAME","AdminID");

回答by Hesam Moosapour

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.

它对我有用。享受。

回答by Maitray Suthar

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,

或者用 PHP 的方式,

$fullname = $customers->firstname. " " .$customers->lastname;