从Laravel Collection仅获取特定属性 [英] Get only specific attributes with from Laravel Collection

查看:65
本文介绍了从Laravel Collection仅获取特定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在查看Laravel Collections的文档和API,但似乎找不到我想要的东西:

I've been reviewing the documentation and API for Laravel Collections, but don't seem to find what I am looking for:

我想从集合中检索带有模型数据的数组,但只能获取指定的属性.

I would like to retrieve an array with model data from a collection, but only get specified attributes.

即类似于Users::toArray('id','name','email'),该集合实际上为用户保留了所有属性,因为它们在其他地方使用,但是在这个特定的位置,我需要一个包含userdata的数组,并且只包含指定的属性.

I.e. something like Users::toArray('id','name','email'), where the collection in fact holds all attributes for the users, because they are used elsewhere, but in this specific place I need an array with userdata, and only the specified attributes.

在Laravel中似乎没有帮助者? -如何最简单地做到这一点?

There does not seem to be a helper for this in Laravel? - How can I do this the easiest way?

推荐答案

您可以结合使用现有的Collection方法来执行此操作.一开始可能很难理解,但应该很容易分解.

You can do this using a combination of existing Collection methods. It may be a little hard to follow at first, but it should be easy enough to break down.

// get your main collection with all the attributes...
$users = Users::get();

// build your second collection with a subset of attributes. this new
// collection will be a collection of plain arrays, not Users models.
$subset = $users->map(function ($user) {
    return collect($user->toArray())
        ->only(['id', 'name', 'email'])
        ->all();
});

说明

首先,map()方法基本上只是遍历Collection,然后将Collection中的每个项目传递给传入的回调.每次回调调用返回的值将构建由map()方法生成的新的Collection.

First, the map() method basically just iterates through the Collection, and passes each item in the Collection to the passed in callback. The value returned from each call of the callback builds the new Collection generated by the map() method.

collect($user->toArray())只是从Users属性中构建了一个新的临时Collection.

collect($user->toArray()) is just building a new, temporary Collection out of the Users attributes.

->only(['id', 'name', 'email'])将临时Collection减少到仅指定的那些属性.

->only(['id', 'name', 'email']) reduces the temporary Collection down to only those attributes specified.

->all()将临时Collection变回普通数组.

->all() turns the temporary Collection back into a plain array.

将所有内容放在一起,您将获得对于用户集合中的每个用户,返回仅包含id,名称和电子邮件属性的数组."

Put it all together and you get "For each user in the users collection, return an array of just the id, name, and email attributes."

Laravel 5.5在模型上添加了only方法,该方法与collect($user->toArray())->only([...])->all()基本上具有相同的功能,因此可以在5.5+中将其稍微简化为:

Laravel 5.5 added an only method on the Model, which basically does the same thing as the collect($user->toArray())->only([...])->all(), so this can be slightly simplified in 5.5+ to:

// get your main collection with all the attributes...
$users = Users::get();

// build your second collection with a subset of attributes. this new
// collection will be a collection of plain arrays, not Users models.
$subset = $users->map(function ($user) {
    return $user->only(['id', 'name', 'email']);
});

如果将其与集合的高级邮件" 在Laravel 5.4中引入,可以进一步简化:

If you combine this with the "higher order messaging" for collections introduced in Laravel 5.4, it can be simplified even further:

// get your main collection with all the attributes...
$users = Users::get();

// build your second collection with a subset of attributes. this new
// collection will be a collection of plain arrays, not Users models.
$subset = $users->map->only(['id', 'name', 'email']);

这篇关于从Laravel Collection仅获取特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆