【问题标题】:Laravel Pluck - how to get more than one fieldLaravel Pluck - 如何获得多个领域
【发布时间】:2017-05-07 06:24:18
【问题描述】:

我怎样才能显示比名字更多的信息?选择并采摘使用 key => value 方法(通过此处的 id),我在哪里写我想看到的其他列(例如 last_name、paygrade 等)? 我想继续使用 eloquent 约定并显示有关所选选项的更多信息。

控制器

$users = User::select('id', 'first_name')->pluck('first_name', 'id');

刀片 {!! Form::select('worker_id', $users, isset($users) ? $users : null, array('class' => 'form-control chosen-select', 'data-placeholder'=> 'Worker...', 'multiple' => 'multiple')) !!}

【问题讨论】:

  • 你认为你为什么需要pluck这里?

标签: php laravel eloquent


【解决方案1】:

你可以用这个:

$users = User::selectRaw('id, CONCAT(first_name," ",last_name) as full_name')->pluck('full_name', 'id');

或者这个:

$users = User::select('id', DB::raw("concat(first_name, ' ', last_name) as full_name")->pluck('full_name', 'id');

【讨论】:

  • 这正是我正在寻找的,你的代码不是很有说服力,请改变它,我会投票作为答案。 $users = User::selectRaw('id, CONCAT(first_name," ",last_name) as full_name')->pluck('full_name', 'id');
  • 你可以像 DB::raw() 一样在 raw 中使用 concat,而其余的代码就像 eloquent跨度>
  • 我做到了,提供了两种写法
猜你喜欢
  • 2015-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-17
  • 1970-01-01
相关资源
最近更新 更多