Eloquent 集合

未匹配的标注
本文档最新版为 10.x,旧版本可能放弃维护,推荐阅读最新版!

Eloquent: 集合

简介

所有返回多个模型查询结果的 Eloquent 方法的返回值都是 Illuminate\Database\Eloquent\Collection 对象的实例,包括通过 get 方法或通过关联关系获取到的结果。 Eloquent 集合对象扩展了 Laravel 的 base collection,因此它自然地继承了许多用于流畅地处理 Eloquent 模型的底层数组的方法。请务必查看 Laravel 集合文档以了解所有这些有用的方法!

而且,所有的集合都可以作为迭代器,你可以像遍历简单的 PHP 数组一样来遍历它们:

use App\Models\User;

$users = User::where('active', 1)->get();

foreach ($users as $user) {
    echo $user->name;
}

不过,集合比数组更加强大,它通过更加直观的接口暴露出可链式调用的 map / reduce 等操作。例如,让我们移除所有未激活的用户并收集剩余用户的名字:

$names = User::all()->reject(function ($user) {
    return $user->active === false;
})->map(function ($user) {
    return $user->name;
});

Eloquent 集合转换

大多数 Eloquent 集合方法会返回新的 Eloquent 集合实例,但是 collapseflattenflipkeyspluckzip 方法除外,它们会返回一个 base collection 实例。 同样,如果 map 操作返回的集合不包括任何 Eloquent 模型, 那么它会被自动转换成集合基类。

可用的方法

所有 Eloquent 的集合都继承了 Laravel collection 对象;因此, 他们也继承了所有集合基类提供的强大的方法。

另外, Illuminate\Database\Eloquent\Collection 类提供了一套上层的方法来帮你管理你的模型集合。大多数方法返回 Illuminate\Database\Eloquent\Collection 实例;然而,也会有一些方法, 例如 modelKeys, 它们会返回基于 Illuminate\Support\Collection 类的实例。

contains($key, $operator = null, $value = null)

contains 方法可用于判断集合中是否包含指定的模型实例。这个方法接收一个主键或者模型实例:

$users->contains(1);

$users->contains(User::find(1));

diff($items)

diff 方法返回不在给定集合中的所有模型:

use App\Models\User;

$users = $users->diff(User::whereIn('id', [1, 2, 3])->get());

except($keys)

except 方法返回给定主键外的所有模型:

$users = $users->except([1, 2, 3]);

find($key)

find 方法查找给定主键的模型。如果 $key 是一个模型实例, find 将会尝试返回与主键匹配的模型。 如果 $key 是一个关联数组, find 将返回所有数组主键匹配的模型:

$users = User::all();

$user = $users->find(1);

fresh($with = [])

fresh 方法用于从数据库中检索集合中每个模型的新实例。此外,还将加载任何指定的关联关系:

$users = $users->fresh();

$users = $users->fresh('comments');

intersect($items)

intersect 方法返回给定集合与当前模型的交集:

use App\Models\User;

$users = $users->intersect(User::whereIn('id', [1, 2, 3])->get());

load($relations)

load 方法为集合中的所有模型加载给定关联关系:

$users->load(['comments', 'posts']);

$users->load('comments.author');

loadMissing($relations)

如果尚未加载关联关系,则 loadMissing 方法将加载集合中所有模型的给定关联关系:

$users->loadMissing(['comments', 'posts']);

$users->loadMissing('comments.author');

modelKeys()

modelKeys 方法返回集合中所有模型的主键:

$users->modelKeys();

// [1, 2, 3, 4, 5]

makeVisible($attributes)

makeVisible 方法 使模型上的隐藏属性可见

$users = $users->makeVisible(['address', 'phone_number']);

makeHidden($attributes)

makeHidden 方法 隐藏模型属性

$users = $users->makeHidden(['address', 'phone_number']);

only($keys)

only 方法返回具有给定主键的所有模型:

$users = $users->only([1, 2, 3]);

toQuery()

toQuery 方法返回一个 Eloquent 查询生成器实例,该实例包含集合模型主键上的 whereIn 约束:

use App\Models\User;

$users = User::where('status', 'VIP')->get();

$users->toQuery()->update([
    'status' => 'Administrator',
]);

unique($key = null, $strict = false)

unique 方法返回集合中所有不重复的模型,若模型在集合中存在相同类型且相同主键的另一模型,该模型将被删除:

$users = $users->unique();

自定义集合

如果你想在与模型交互时使用一个自定义的 Collection 对象,你可以通过在模型中定义 newCollection 方法来实现:

<?php

namespace App\Models;

use App\Support\UserCollection;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * Create a new Eloquent Collection instance.
     *
     * @param  array  $models
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function newCollection(array $models = [])
    {
        return new UserCollection($models);
    }
}

一旦在模型中定义了一个 newCollection 方法,每当 Eloquent 需要返回一个 Illuminate\Database\Eloquent\Collection 实例的时候,将会返回自定义集合的实例取代之。如果你想使每一个模型都使用自定义的集合,可以在一个模型基类中定义一个 newCollection 方法,然后让其它模型派生于此基类。

本文章首发在 LearnKu.com 网站上。

本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://learnku.com/docs/laravel/9.x/elo...

译文地址:https://learnku.com/docs/laravel/9.x/elo...

上一篇 下一篇
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
贡献者:4
讨论数量: 2
发起讨论 只看当前版本


ChoChik
为什么有些方法文档中找不到??
0 个点赞 | 5 个回复 | 问答 | 课程版本 5.5
circle
内容重复小 bug 第二处?
0 个点赞 | 0 个回复 | 问答 | 课程版本 5.6