Non*_*one 9 laravel laravel-5 laravel-5.2
当我使用多列的pluck时,我得到这个:
{"Kreis 1 \/ Altstadt":"City","Kreis 2":"Enge","Kreis 3":"Sihifeld","Kreis 4":"Hard","Kreis 5 \/ Industriequartier":"Escher Wyss","Kreis 6":"Oberstrass","Kreis 7":"Witikon","Kreis 8 \/ Reisbach":"Weinegg","Kreis 9":"Altstetten","Kreis 10":"Wipkingen","Kreis 11":"Seebach","Kreis 12 \/ Schwamendingen":"Hirzenbach"
Run Code Online (Sandbox Code Playgroud)
但我需要这个吗?
["Rathaus","Hochschulen","Lindenhof","City","Wollishofen","Leimbach","Enge","Alt-Wiedikon","Friesenberg","Sihifeld","Werd","Langstrasse","Hard","Gewerbechule","Escher Wyss","Unterstrass","Oberstrass","Fluntern","Hottingen","Hirslanden","Witikon","Seefeld","M\u00fchlebach","Weinegg","Albisrieden","Altstetten","H\u00f6ngg","Wipkingen","Affoltern","Oerlikon","Seebach","Saatlen","Schwamendingen-Mitte","Hirzenbach"]
Run Code Online (Sandbox Code Playgroud)
任何建议我怎么能这样做?这是我的方法:
public function autocomplete_districts(Request $request)
{
$district = $request->input('query');
// $ass = /DB::table('districts')->select(array('district', 'region'))->get();
// dd($ass);
$data = Districts::whereRaw('LOWER(district) like ?', [strtolower('%'.$district . '%')])->orWhereRaw('LOWER(region) like ?', [strtolower('%'.$district . '%')])->pluck('region','district');
return response()->json($data);
}
Run Code Online (Sandbox Code Playgroud)
Lea*_*ner 18
您应该使用select()
,get()
然后根据需要修改对象.
所以代替:->pluck('region','district');
使用:->select('region','district')->get();
pluck()
当您只需要一列的值时,建议您使用.
并且尽可能地,你应该让你的模型单数形式而不是复数(区) - 遵循Laravel命名法.
Ork*_*nov 18
就我而言,我想从 Eloquent 模型数组中提取 2 个值,这有效:
$models->map->only(['state', 'note'])->values()
Run Code Online (Sandbox Code Playgroud)
这是较短的版本
$models->map(fn($model) => $model->only(['state', 'note']))->values()
Run Code Online (Sandbox Code Playgroud)
这就是采弹的原理。而是尝试这个。
$data = Districts::whereRaw('LOWER(district) like ?', [strtolower('%'.$district . '%')])->orWhereRaw('LOWER(region) like ?', [strtolower('%'.$district . '%')])->select('region', 'district')->get();
$data = collect($data->toArray())->flatten()->all();
Run Code Online (Sandbox Code Playgroud)
小智 6
我在 LARAVEL 5.6 中的解决方案:
您好,我刚刚遇到了同样的问题,我需要将 2 列合并到 1 个选择列表中。我的数据库有 2 列用户:名字和姓氏。我需要一个选择框,其中显示用户全名,并将 ID 作为值。这就是我使用 pluck() 方法修复它的方法:
在用户模型中,我创建了一个全名访问器函数:
public function getNameAttribute() {
return ucwords($this->last_name . ' ' . $this->first_name);
}
Run Code Online (Sandbox Code Playgroud)
之后,为了使用全名和相应的数据库 ID 作为值填充选择列表,我在返回视图的控制器中使用了此代码(不显示已存档的用户,但如果您愿意,您可以更改查询的开头,最重要的是 get() 和 pluck() 函数:
$users = User::whereNull('archived_at')
->orderBy('last_name')
->get(['id','first_name','last_name'])
->pluck('name','id');
return view('your.view', compact('users'));
Run Code Online (Sandbox Code Playgroud)
现在您可以在选择列表中使用 $users 了!
因此,首先,您从数据库中获取所需的所有值,之后您可以使用定义在 PLUCK 方法中使用的任何访问器属性,
只要访问器所需的所有列都在 GET 中即可;-)
这是我经常遇到的一个问题,并促使我创建了以下可用于模型或阵列的解决方案。
还支持点语法,可根据需要创建多维数组。
在AppServiceProvider
(或您选择的任何提供程序)中注册此宏:
use Illuminate\Support\Arr;
/**
* Similar to pluck, with the exception that it can 'pluck' more than one column.
* This method can be used on either Eloquent models or arrays.
* @param string|array $cols Set the columns to be selected.
* @return Collection A new collection consisting of only the specified columns.
*/
Collection::macro('pick', function ($cols = ['*']) {
$cols = is_array($cols) ? $cols : func_get_args();
$obj = clone $this;
// Just return the entire collection if the asterisk is found.
if (in_array('*', $cols)) {
return $this;
}
return $obj->transform(function ($value) use ($cols) {
$ret = [];
foreach ($cols as $col) {
// This will enable us to treat the column as a if it is a
// database query in order to rename our column.
$name = $col;
if (preg_match('/(.*) as (.*)/i', $col, $matches)) {
$col = $matches[1];
$name = $matches[2];
}
// If we use the asterisk then it will assign that as a key,
// but that is almost certainly **not** what the user
// intends to do.
$name = str_replace('.*.', '.', $name);
// We do it this way so that we can utilise the dot notation
// to set and get the data.
Arr::set($ret, $name, data_get($value, $col));
}
return $ret;
});
});
Run Code Online (Sandbox Code Playgroud)
然后可以通过以下方式使用它:
$a = collect([
['first' => 1, 'second' => 2, 'third' => 3],
['first' => 1, 'second' => 2, 'third' => 3]
]);
$b = $a->pick('first', 'third'); // returns [['first' => 1, 'third' => 3], ['first' => 1, 'third' => 3]]
Run Code Online (Sandbox Code Playgroud)
或者另外,在您可能拥有的任何型号上:
$users = User::all();
$new = $users->pick('name', 'username', 'email');
// Might return something like:
// [
// ['name' => 'John Doe', 'username' => 'john', 'email' => 'john@email.com'],
// ['name' => 'Jane Doe', 'username' => 'jane', 'email' => 'jane@email.com'],
// ['name' => 'Joe Bloggs', 'username' => 'joe', 'email' => 'joe@email.com'],
// ]
Run Code Online (Sandbox Code Playgroud)
也可以使用点表示法以及使用as [other name]
语法来引用任何关系:
$users = User::all();
$new = $users->pick('name as fullname', 'email', 'posts.comments');
// Might return something like:
// [
// ['fullname' => 'John Doe', 'email' => 'john@email.com', 'posts' => [...]],
// ['fullname' => 'Jane Doe', 'email' => 'jane@email.com', 'posts' => [...]],
// ['fullname' => 'Joe Bloggs', 'email' => 'joe@email.com', 'posts' => [...]],
// ]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22629 次 |
最近记录: |