Laravel - 提取变异属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42077013/
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
Laravel - pluck mutated attribute
提问by D.Meganoski
I have run into an issue, and it is really only a minor inconvenience, but...
我遇到了一个问题,这真的只是一个小小的不便,但是......
Basically what I need is an associative array for a select box. Normally, this would be achieved using the pluck()
function.
基本上我需要的是一个选择框的关联数组。通常,这将使用该pluck()
函数来实现。
The problem is, the attribute that I want to use as the 'text' does not actually exist in the database, it is a mutator that combines two fields into one.
问题是,我想用作“文本”的属性实际上并不存在于数据库中,它是一个将两个字段合并为一个的 mutator。
public function getNameAttribute() {
return $this->first_name . ' ' . $this->last_name;
}
I know that adding the 'name'
field to the $appends
array on the model will include that field when casting the model to array, however this does not seem to work with pluck()
我知道在将模型转换为数组时,将'name'
字段添加到$appends
模型上的数组将包括该字段,但这似乎不适用于pluck()
Is there a simple way to achieve want I want? Function or declaration I'm missing? Anything more eloquent than manually looping over the collection and creating my own associate array?
有没有一种简单的方法可以实现我想要的?我缺少的功能或声明?有什么比手动循环遍历集合并创建我自己的关联数组更有说服力的吗?
Update
更新
I'm an idiot. I was passing an array to pluck instead of two parameters. Apparently pluck does utilize the $appends
attribute. Note, this only works when working with collections:
我是个白痴。我正在传递一个数组来 pluck 而不是两个参数。显然 pluck 确实利用了该$appends
属性。请注意,这仅在处理集合时有效:
$collection->pluck('mutatedValue', 'key');
NOTthe query builder
不是查询构建器
$queryBuilder->pluck('mutatedValue', 'id');
采纳答案by bishop
You can dynamically append the attribute to the objects in your collection:
您可以将属性动态附加到集合中的对象:
$users->each(function ($model) { $model->setAppends(['name']); });
$users->pluck('name');
This has the nice advantage of not requiring 'name'
always be part of your array data, while allowing your collection to use name
just in time.
这有一个很好的优点,即不需要'name'
始终成为数组数据的一部分,同时允许您的集合及时使用name
。
回答by Zach Chisholm
I was looking for an answer to this and ended up with a different result so I thought I'd share.
我一直在寻找这个问题的答案,最终得到了不同的结果,所以我想我会分享。
User::get()->pluck('name');
If you get the object first, your mutated attribute will be accessible.
如果您首先获得对象,您的变异属性将是可访问的。
回答by online Thomas
You can query with eloquent and use
您可以使用 eloquent 进行查询并使用
$limited_select = User::all(['id', 'first_name', 'last_name']);
$limited_select = array_flip($limited_select);
I assumed you wanted to prevent a select *
when possible.
我假设您想select *
在可能的情况下阻止 a 。
generating the select you can do something like this in blade:
生成选择你可以在刀片中做这样的事情:
<select>
@foreach($limited_select as $user)
<option value={{$user->id}}> {{$user->getNameAttribute() }} </option>
@endforeach
</select>
This would work because getNameAttribute()
inside your User Model
wil act as an accessor.
这会起作用,因为getNameAttribute()
在您的内部将User Model
充当访问器。
Your second option could be using a raw query that has a concat, but I think the eloquent way is more elegant.
您的第二个选择可能是使用具有 concat 的原始查询,但我认为雄辩的方式更优雅。
回答by Waiyl Karim
You could use a collection
method such as map()
:
您可以使用以下collection
方法map()
:
<?php
// ...
User::get()
->map(function($user) {
return [
'id' => $user->id,
'text' => $user->myCustomAttribute
];
})
->pluck('text', 'id')
->all();