Laravel 4 - 从集合中获取属性数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18090647/
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 4 - Get Array of Attributes from Collection
提问by severin
I have a collection of objects. Let's say the objects are tags:
我有一组对象。假设对象是标签:
$tags = Tag::all();
I want to retrieve a certain attribute for each tag, say its name. Of course I can do
我想为每个标签检索某个属性,比如它的名字。我当然可以
foreach ($tags as $tag) {
$tag_names[] = $tag->name;
}
But is there a more laravelish solution to this problem?
但是这个问题有没有更简单的解决方案呢?
Something like $tags->name
?
像$tags->name
什么?
回答by severin
Collections have a lists
method similar to the method for tables described by @Gadoma. It returns an array containing the value of a given attribute for each item in the collection.
集合的lists
方法类似于@Gadoma 描述的表格方法。它返回一个数组,其中包含集合中每个项目的给定属性的值。
To retrieve the desired array of names from my collection of tags I can simply use:
要从我的标签集合中检索所需的名称数组,我可以简单地使用:
$tags->lists('name');
Update:As of laravel 5.2lists
is replaced by pluck
.
更新:从laravel 5.2 开始lists
被替换为pluck
.
$tags->pluck('name');
More specifically, the laravel 5.2 upgrade guidestates that "[t]he lists
method on the Collection, query builder and Eloquent query builder objects has been renamed to pluck
. The method signature remains the same."
更具体地说,laravel 5.2 升级指南指出“lists
集合、查询构建器和 Eloquent 查询构建器对象上pluck
的方法已重命名为。方法签名保持不变。”
回答by Gadoma
Yep, you can do it nice and easily. As the Laravel 4 Documentation states, you can do
是的,您可以轻松轻松地做到这一点。正如Laravel 4 文档所述,您可以
Retrieving All Rows From A Table
从表中检索所有行
$users = DB::table('users')->get();
foreach ($users as $user)
{
var_dump($user->name);
}
Retrieving A Single Row From A Table
从表中检索单行
$user = DB::table('users')->where('name', 'John')->first();
var_dump($user->name);
Retrieving A Single Column From A Row
从一行中检索单列
$name = DB::table('users')->where('name', 'John')->pluck('name');
Retrieving A List Of Column Values
检索列值列表
$roles = DB::table('roles')->lists('title');
This method will return an array of role titles.You may also specify a custom key column for the returned array:
此方法将返回一个角色头衔数组。您还可以为返回的数组指定自定义键列:
$roles = DB::table('roles')->lists('title', 'name');
Specifying A Select Clause
指定选择子句
$users = DB::table('users')->select('name', 'email')->get();
$users = DB::table('users')->distinct()->get();
$users = DB::table('users')->select('name as user_name')->get();
EDIT:
编辑:
The above examples show how to access data with the help of Laravel's fluent query builder. If you are using models you can access the data with Laravel's Eloquent ORM
上面的例子展示了如何在 Laravel 的流畅查询构建器的帮助下访问数据。如果您正在使用模型,您可以使用Laravel 的 Eloquent ORM访问数据
Because Eloquent is internaly using the query builder, you can without any problem do the following things:
因为 Eloquent 在内部使用查询构建器,所以您可以毫无问题地执行以下操作:
$tag_names = $tags->lists('tag_name_label', 'tag_name_column')->get();
which could be also done with:
也可以通过以下方式完成:
$tag_names = DB::table('tags')->lists('tag_name_label', 'tag_name_column')->get();
回答by Erik Aybar
Here are a few snippets from my own experimentation on the matter this morning. I only wish (and maybe someone else knows the solution) that the Collection had a $collection->distinct() method, so I could easily generate a list of column values based on an already filtered collection.
以下是我今天早上自己在这个问题上的实验中的一些片段。我只希望(也许其他人知道解决方案)集合有一个 $collection->distinct() 方法,这样我就可以根据已经过滤的集合轻松生成列值列表。
Thoughts?
想法?
I hope these snippets help clarify some alternative options for generating a list of unique values from a Table, Collection, and Eloquent Model.
我希望这些片段有助于阐明从表、集合和 Eloquent 模型生成唯一值列表的一些替代选项。
Using a Collection (Happy)
使用集合(快乐)
/**
* Method A
* Store Collection to reduce queries when building multiple lists
*/
$people = Person::get();
$cities = array_unique( $people->lists('city') );
$states = array_unique( $people->lists('state') );
// etc...
Using an Eloquent Model (Happier)
使用 Eloquent 模型(更快乐)
/**
* Method B
* Utilize the Eloquent model's methods
* One query per list
*/
// This will return an array of unique cities present in the list
$cities = Person::distinct()->lists('city');
$states = Person::distinct()->lists('state');
Using an Eloquent Model PLUS Caching (Happiest)
使用 Eloquent 模型 PLUS 缓存(最快乐)
/**
* Method C
* Utilize the Eloquent model's methods PLUS the built in Caching
* Queries only run once expiry is reached
*/
$expiry = 60; // One Hour
$cities = Person::remember($expiry)->distinct()->lists('city');
$states = Person::remember($expiry)->distinct()->lists('state');
I would love to hear some alternatives to this if you guys have one!
如果你们有的话,我很想听到一些替代方案!
@ErikOnTheWeb
@ErikOnTheWeb
回答by Jason Lewis
You could use array_column
for this (it's a PHP 5.5 function but Laravel has a helper function that replicates the behavior, for the most part).
你可以使用array_column
它(它是一个 PHP 5.5 函数,但 Laravel 有一个辅助函数可以复制大部分行为)。
Something like this should suffice.
像这样的东西应该就足够了。
$tag_names = array_column($tags->toArray(), 'name');