【问题标题】:Laravel pluck but combining first name + last name for selectLaravel 采摘但结合名字+姓氏进行选择
【发布时间】:2017-11-12 12:27:27
【问题描述】:

在 Laravel/Vue 项目中使用 select2,需要返回如下格式的 JSON:

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

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

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

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

我该怎么做?

【问题讨论】:

    标签: laravel laravel-5 eloquent jquery-select2


    【解决方案1】:

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

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

    我还没有测试过,但这可以工作:

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

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

    然后尝试:

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

    $customers = Customer::all()->pluck('full_name', 'id');
    
    • 附带说明,为了提高性能,最好使用Customer::all(['id', 'first_name', 'last_name'])->pluck(...),这样我们就不会从数据库中提取不必要的列。

    希望这会有所帮助。

    更新日期:- 2021 年 8 月 26 日 如果我们使用计算属性accessor functionality,那么请注意一件重要的事情......

    Laravel 访问器功能在从数据库中获取数据之后起作用。所以我们必须在Query结束时声明"pluck(accessorName)"....

    例如:-

    错误的方法:-

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

    在上述两个查询中,如果 DataTable 中没有 full_name 字段,则会出现 Unknown column error

    正确的方法:-

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

    在上述两个查询中,即使您在 DataTable 中没有 full_name 字段,它也能完美运行

    【讨论】:

    • 感谢您解释这仅适用于集合,这正是它不适用于我的原因。赞成!
    【解决方案2】:

    你可以这样做,

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

    或者你也这样做,

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

    或者用PHP方式,

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

    【讨论】:

    • 它给了我 SQLSTATE[42S22]:找不到列:1054 未知列 'CONCAT...
    【解决方案3】:

    对我来说很有效

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

    【讨论】:

      【解决方案4】:

      用户模型:

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

      获取查询将导致收集:

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

      为了将变量从对象转换为数组:

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

      它对我有用。享受吧。

      【讨论】:

        【解决方案5】:
           /**
         * Get the full name of the user.
         *
         * @return string
         */
        public function getFullNameAttribute()
        {
            return "{$this->first_name} {$this->last_name}";
        }
        

        并使用这个采摘

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

        喜欢这个

           public static function list()
        {
            return Cache::tags('usersCustomers')->rememberForever(md5('usersCustomers.list:' . locale()), function () {
                return self::all()->sortBy('id')->pluck('full_name', 'id');
            });
        }
        

        【讨论】:

          【解决方案6】:

          使用此代码。希望它会奏效。我用这段代码解决了这个问题

          User::select(DB::raw("CONCAT(first_name, ' ', last_name) AS full_name"),"id")->pluck("full_name","id");
          

          【讨论】:

            【解决方案7】:

            ** 使用此代码。希望它会奏效。我用这段代码解决了这个问题**

                   User::select(DB::raw("CONCAT(first_name, ' ', last_name) AS 
                   full_name"),"id")->pluck("full_name","id");
            

            【讨论】:

              猜你喜欢
              • 2017-01-22
              • 1970-01-01
              • 2016-02-22
              • 1970-01-01
              • 2010-10-26
              • 1970-01-01
              • 1970-01-01
              • 2017-06-11
              • 1970-01-01
              相关资源
              最近更新 更多