Skip to content

Eloquent:关联关系

简介

数据库表通常相互关联。例如,一篇博客文章可能有多条评论,或者一个订单可能关联到下单的用户。Eloquent 使管理和使用这些关联关系变得简单,并支持多种常见的关联关系:

定义关联关系

Eloquent 关联关系被定义为 Eloquent 模型类上的方法。由于关联关系也充当强大的查询构建器,将关联关系定义为方法提供了强大的方法链式调用和查询功能。例如,我们可以在这个 posts 关联关系上链式添加额外的查询约束:

php
$user->posts()->where('active', 1)->get();

但在深入使用关联关系之前,让我们先学习如何定义 Eloquent 支持的每种关联关系类型。

一对一 / Has One

一对一关联关系是一种非常基本的数据库关联类型。例如,一个 User 模型可能关联一个 Phone 模型。要定义此关联关系,我们将在 User 模型上放置一个 phone 方法。phone 方法应调用 hasOne 方法并返回其结果。hasOne 方法通过模型的 Illuminate\Database\Eloquent\Model 基类提供给你的模型:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;

class User extends Model
{
    /**
     * Get the phone associated with the user.
     */
    public function phone(): HasOne
    {
        return $this->hasOne(Phone::class);
    }
}

传递给 hasOne 方法的第一个参数是关联模型类的名称。一旦定义了关联关系,我们就可以使用 Eloquent 的动态属性来检索关联记录。动态属性允许你像访问模型上定义的属性一样访问关联关系方法:

php
$phone = User::find(1)->phone;

Eloquent 根据父模型名称确定关联关系的外键。在这种情况下,Phone 模型会自动假设有一个 user_id 外键。如果你想覆盖此约定,可以向 hasOne 方法传递第二个参数:

php
return $this->hasOne(Phone::class, 'foreign_key');

此外,Eloquent 假设外键应该有一个与父模型的主键列匹配的值。换句话说,Eloquent 将在 Phone 记录的 user_id 列中查找用户 id 列的值。如果你希望关联关系使用 id 或模型主键以外的主键值,可以向 hasOne 方法传递第三个参数:

php
return $this->hasOne(Phone::class, 'foreign_key', 'local_key');

定义反向关联关系

所以,我们可以从 User 模型访问 Phone 模型。接下来,让我们在 Phone 模型上定义一个关联关系,让我们可以访问拥有该电话的用户。我们可以使用 belongsTo 方法定义 hasOne 关联关系的反向:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Phone extends Model
{
    /**
     * Get the user that owns the phone.
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }
}

调用 user 方法时,Eloquent 将尝试查找 idPhone 模型上的 user_id 列匹配的 User 模型。

Eloquent 通过检查关联关系方法的名称并在方法名后添加 _id 后缀来确定外键名称。因此,在这种情况下,Eloquent 假设 Phone 模型有一个 user_id 列。但是,如果 Phone 模型上的外键不是 user_id,你可以将自定义键名作为第二个参数传递给 belongsTo 方法:

php
/**
 * Get the user that owns the phone.
 */
public function user(): BelongsTo
{
    return $this->belongsTo(User::class, 'foreign_key');
}

如果父模型不使用 id 作为其主键,或者你希望使用不同的列查找关联模型,可以向 belongsTo 方法传递第三个参数来指定父表的自定义键:

php
/**
 * Get the user that owns the phone.
 */
public function user(): BelongsTo
{
    return $this->belongsTo(User::class, 'foreign_key', 'owner_key');
}

一对多 / Has Many

一对多关联关系用于定义单个模型是一个或多个子模型的父模型的关联关系。例如,一篇博客文章可能有无限数量的评论。与所有其他 Eloquent 关联关系一样,一对多关联关系通过在 Eloquent 模型上定义方法来定义:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Post extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function comments(): HasMany
    {
        return $this->hasMany(Comment::class);
    }
}

请记住,Eloquent 会自动为 Comment 模型确定正确的外键列。按照约定,Eloquent 会取父模型的"蛇形命名法"名称并在其后加上 _id。因此,在此示例中,Eloquent 将假设 Comment 模型上的外键列为 post_id

一旦定义了关联关系方法,我们可以通过访问 comments 属性来访问相关评论的集合。请记住,由于 Eloquent 提供了"动态关联属性",我们可以像访问模型上定义的属性一样访问关联关系方法:

php
use App\Models\Post;

$comments = Post::find(1)->comments;

foreach ($comments as $comment) {
    // ...
}

由于所有关联关系也充当查询构建器,你可以通过调用 comments 方法并继续在查询上链式添加条件来添加更多约束:

php
$comment = Post::find(1)->comments()
    ->where('title', 'foo')
    ->first();

hasOne 方法一样,你也可以通过向 hasMany 方法传递额外参数来覆盖外键和本地键:

php
return $this->hasMany(Comment::class, 'foreign_key');

return $this->hasMany(Comment::class, 'foreign_key', 'local_key');

在子模型上自动填充父模型

即使使用了 Eloquent 预加载,如果你在遍历子模型时尝试从子模型访问父模型,仍然会出现"N + 1"查询问题:

php
$posts = Post::with('comments')->get();

foreach ($posts as $post) {
    foreach ($post->comments as $comment) {
        echo $comment->post->title;
    }
}

在上面的示例中,即使为每个 Post 模型预加载了评论,但 Eloquent 不会自动在每个子 Comment 模型上填充父 Post,因此引入了"N + 1"查询问题。

如果你希望 Eloquent 自动在子模型上填充父模型,你可以在定义 hasMany 关联关系时调用 chaperone 方法:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Post extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function comments(): HasMany
    {
        return $this->hasMany(Comment::class)->chaperone();
    }
}

或者,如果你想在运行时选择自动父模型填充,可以在预加载关联关系时调用 chaperone 方法:

php
use App\Models\Post;

$posts = Post::with([
    'comments' => fn ($comments) => $comments->chaperone(),
])->get();

一对多(反向)/ Belongs To

现在我们可以访问文章的所有评论了,让我们定义一个关联关系,允许评论访问其父文章。要定义 hasMany 关联关系的反向,请在子模型上定义一个调用 belongsTo 方法的关联关系方法:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Comment extends Model
{
    /**
     * Get the post that owns the comment.
     */
    public function post(): BelongsTo
    {
        return $this->belongsTo(Post::class);
    }
}

一旦定义了关联关系,我们可以通过访问 post"动态关联属性"来检索评论的父文章:

php
use App\Models\Comment;

$comment = Comment::find(1);

return $comment->post->title;

在上面的示例中,Eloquent 将尝试查找 idComment 模型上的 post_id 列匹配的 Post 模型。

Eloquent 通过检查关联关系方法的名称并在方法名后添加 _ 加上父模型主键列的名称来确定默认外键名称。因此,在此示例中,Eloquent 将假设 Post 模型在 comments 表上的外键为 post_id

但是,如果你的关联关系的外键不遵循这些约定,你可以将自定义外键名称作为第二个参数传递给 belongsTo 方法:

php
/**
 * Get the post that owns the comment.
 */
public function post(): BelongsTo
{
    return $this->belongsTo(Post::class, 'foreign_key');
}

如果你的父模型不使用 id 作为其主键,或者你希望使用不同的列查找关联模型,可以向 belongsTo 方法传递第三个参数来指定父表的自定义键:

php
/**
 * Get the post that owns the comment.
 */
public function post(): BelongsTo
{
    return $this->belongsTo(Post::class, 'foreign_key', 'owner_key');
}

默认模型

belongsTohasOnehasOneThroughmorphOne 关联关系允许你定义一个在给定关联关系为 null 时返回的默认模型。此模式通常被称为空对象模式,可以帮助消除代码中的条件检查。在以下示例中,如果没有用户附加到 Post 模型,user 关联将返回一个空的 App\Models\User 模型:

php
/**
 * Get the author of the post.
 */
public function user(): BelongsTo
{
    return $this->belongsTo(User::class)->withDefault();
}

要使用属性填充默认模型,你可以向 withDefault 方法传递数组或闭包:

php
/**
 * Get the author of the post.
 */
public function user(): BelongsTo
{
    return $this->belongsTo(User::class)->withDefault([
        'name' => 'Guest Author',
    ]);
}

/**
 * Get the author of the post.
 */
public function user(): BelongsTo
{
    return $this->belongsTo(User::class)->withDefault(function (User $user, Post $post) {
        $user->name = 'Guest Author';
    });
}

查询 Belongs To 关联关系

查询"belongs to"关联关系的子模型时,你可以手动构建 where 子句来检索对应的 Eloquent 模型:

php
use App\Models\Post;

$posts = Post::where('user_id', $user->id)->get();

但是,你可能会发现使用 whereBelongsTo 方法更方便,它会自动确定给定模型的正确关联关系和外键:

php
$posts = Post::whereBelongsTo($user)->get();

你也可以向 whereBelongsTo 方法提供集合实例。这样做时,Laravel 将检索属于集合中任何父模型的模型:

php
$users = User::where('vip', true)->get();

$posts = Post::whereBelongsTo($users)->get();

默认情况下,Laravel 将根据模型的类名确定与给定模型关联的关联关系;但是,你可以通过将关联关系名称作为第二个参数传递给 whereBelongsTo 方法来手动指定:

php
$posts = Post::whereBelongsTo($user, 'author')->get();

Has One of Many

有时一个模型可能有许多关联模型,但你想轻松检索关联关系的"最新"或"最旧"的关联模型。例如,一个 User 模型可能关联许多 Order 模型,但你想定义一种便捷的方式来与用户最近下的订单交互。你可以使用 hasOne 关联关系类型结合 ofMany 方法来实现:

php
/**
 * Get the user's most recent order.
 */
public function latestOrder(): HasOne
{
    return $this->hasOne(Order::class)->latestOfMany();
}

同样,你可以定义一个方法来检索关联关系的"最旧"或第一个关联模型:

php
/**
 * Get the user's oldest order.
 */
public function oldestOrder(): HasOne
{
    return $this->hasOne(Order::class)->oldestOfMany();
}

默认情况下,latestOfManyoldestOfMany 方法将根据模型的主键检索最新或最旧的关联模型,主键必须是可排序的。但有时你可能希望使用不同的排序标准从较大的关联关系中检索单个模型。

例如,使用 ofMany 方法,你可以检索用户最贵的订单。ofMany 方法接受可排序列作为其第一个参数,以及查询关联模型时要应用的聚合函数(minmax):

php
/**
 * Get the user's largest order.
 */
public function largestOrder(): HasOne
{
    return $this->hasOne(Order::class)->ofMany('price', 'max');
}

WARNING

由于 PostgreSQL 不支持对 UUID 列执行 MAX 函数,因此目前无法将 one-of-many 关联关系与 PostgreSQL UUID 列结合使用。

将"多"关联关系转换为 Has One 关联关系

通常,当使用 latestOfManyoldestOfManyofMany 方法检索单个模型时,你已经为同一模型定义了"has many"关联关系。为方便起见,Laravel 允许你通过在关联关系上调用 one 方法轻松将此关联关系转换为"has one"关联关系:

php
/**
 * Get the user's orders.
 */
public function orders(): HasMany
{
    return $this->hasMany(Order::class);
}

/**
 * Get the user's largest order.
 */
public function largestOrder(): HasOne
{
    return $this->orders()->one()->ofMany('price', 'max');
}

你也可以使用 one 方法将 HasManyThrough 关联关系转换为 HasOneThrough 关联关系:

php
public function latestDeployment(): HasOneThrough
{
    return $this->deployments()->one()->latestOfMany();
}

高级 Has One of Many 关联关系

可以构建更高级的"has one of many"关联关系。例如,Product 模型可能有许多关联的 Price 模型,即使发布了新定价,这些模型也会保留在系统中。此外,产品的新定价数据可能能够提前发布,以便在将来的日期通过 published_at 列生效。

因此,总而言之,我们需要检索最新发布的定价,其中发布日期不在未来。此外,如果两个价格具有相同的发布日期,我们将优先选择 ID 最大的价格。为此,我们必须向 ofMany 方法传递一个包含确定最新价格的可排序列的数组。此外,将提供一个闭包作为 ofMany 方法的第二个参数。此闭包将负责向关联关系查询添加额外的发布日期约束:

php
/**
 * Get the current pricing for the product.
 */
public function currentPricing(): HasOne
{
    return $this->hasOne(Price::class)->ofMany([
        'published_at' => 'max',
        'id' => 'max',
    ], function (Builder $query) {
        $query->where('published_at', '<', now());
    });
}

远程一对一

"远程一对一"关联关系定义了与另一个模型的一对一关联关系。但是,此关联关系表示声明模型可以通过 经过 第三个模型与另一个模型的一个实例匹配。

例如,在汽车维修店应用程序中,每个 Mechanic 模型可能关联一个 Car 模型,每个 Car 模型可能关联一个 Owner 模型。虽然技师和车主在数据库中没有直接关联,但技师可以 通过 Car 模型访问车主。让我们看看定义此关联关系所需的表:

text
mechanics
    id - integer
    name - string

cars
    id - integer
    model - string
    mechanic_id - integer

owners
    id - integer
    name - string
    car_id - integer

现在我们已经检查了关联关系的表结构,让我们在 Mechanic 模型上定义关联关系:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;

class Mechanic extends Model
{
    /**
     * Get the car's owner.
     */
    public function carOwner(): HasOneThrough
    {
        return $this->hasOneThrough(Owner::class, Car::class);
    }
}

传递给 hasOneThrough 方法的第一个参数是我们希望访问的最终模型的名称,第二个参数是中间模型的名称。

或者,如果关联关系中涉及的所有模型上已经定义了相关的关联关系,你可以通过调用 through 方法并提供这些关联关系的名称来流畅地定义"远程一对一"关联关系:

php
// 基于字符串的语法...
return $this->through('cars')->has('owner');

// 动态语法...
return $this->throughCars()->hasOwner();

键约定

在执行关联关系查询时,将使用典型的 Eloquent 外键约定。如果你想自定义关联关系的键,可以将它们作为第三个和第四个参数传递给 hasOneThrough 方法。第三个参数是中间模型上的外键名称。第四个参数是最终模型上的外键名称。第五个参数是本地键,第六个参数是中间模型的本地键:

php
class Mechanic extends Model
{
    /**
     * Get the car's owner.
     */
    public function carOwner(): HasOneThrough
    {
        return $this->hasOneThrough(
            Owner::class,
            Car::class,
            'mechanic_id', // cars 表上的外键...
            'car_id', // owners 表上的外键...
            'id', // mechanics 表上的本地键...
            'id' // cars 表上的本地键...
        );
    }
}

或者,如前所述,如果关联关系中涉及的所有模型上已经定义了相关的关联关系,你可以通过调用 through 方法并提供这些关联关系的名称来流畅地定义"远程一对一"关联关系。这种方法的优势在于可以重用已在现有关联关系上定义的键约定:

php
// 基于字符串的语法...
return $this->through('cars')->has('owner');

// 动态语法...
return $this->throughCars()->hasOwner();

远程一对多

"远程一对多"关联关系提供了一种便捷的方式通过中间关联关系访问远程关联。例如,假设我们正在构建一个像 Laravel Cloud 这样的部署平台。一个 Application 模型可能通过中间的 Environment 模型访问许多 Deployment 模型。使用此示例,你可以轻松收集给定应用程序的所有部署。让我们看看定义此关联关系所需的表:

text
applications
    id - integer
    name - string

environments
    id - integer
    application_id - integer
    name - string

deployments
    id - integer
    environment_id - integer
    commit_hash - string

现在我们已经检查了关联关系的表结构,让我们在 Application 模型上定义关联关系:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class Application extends Model
{
    /**
     * Get all of the deployments for the application.
     */
    public function deployments(): HasManyThrough
    {
        return $this->hasManyThrough(Deployment::class, Environment::class);
    }
}

传递给 hasManyThrough 方法的第一个参数是我们希望访问的最终模型的名称,第二个参数是中间模型的名称。

或者,如果关联关系中涉及的所有模型上已经定义了相关的关联关系,你可以通过调用 through 方法并提供这些关联关系的名称来流畅地定义"远程一对多"关联关系:

php
// 基于字符串的语法...
return $this->through('environments')->has('deployments');

// 动态语法...
return $this->throughEnvironments()->hasDeployments();

虽然 Deployment 模型的表不包含 application_id 列,但 hasManyThrough 关联关系通过 $application->deployments 提供了对应用程序部署的访问。为了检索这些模型,Eloquent 检查中间 Environment 模型表上的 application_id 列。找到相关的环境 ID 后,使用它们来查询 Deployment 模型的表。

键约定

在执行关联关系查询时,将使用典型的 Eloquent 外键约定。如果你想自定义关联关系的键,可以将它们作为第三个和第四个参数传递给 hasManyThrough 方法:

php
class Application extends Model
{
    public function deployments(): HasManyThrough
    {
        return $this->hasManyThrough(
            Deployment::class,
            Environment::class,
            'application_id', // environments 表上的外键...
            'environment_id', // deployments 表上的外键...
            'id', // applications 表上的本地键...
            'id' // environments 表上的本地键...
        );
    }
}

或者,如前所述,你可以通过调用 through 方法来重用已在现有关联关系上定义的键约定:

php
// 基于字符串的语法...
return $this->through('environments')->has('deployments');

// 动态语法...
return $this->throughEnvironments()->hasDeployments();

带作用域的关联关系

在模型上添加约束关联关系的额外方法是很常见的。例如,你可能在 User 模型上添加一个 featuredPosts 方法,它使用额外的 where 约束来约束更广泛的 posts 关联关系:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class User extends Model
{
    /**
     * Get the user's posts.
     */
    public function posts(): HasMany
    {
        return $this->hasMany(Post::class)->latest();
    }

    /**
     * Get the user's featured posts.
     */
    public function featuredPosts(): HasMany
    {
        return $this->posts()->where('featured', true);
    }
}

但是,如果你尝试通过 featuredPosts 方法创建模型,其 featured 属性不会被设置为 true。如果你想通过关联关系方法创建模型并同时指定应添加到通过该关联关系创建的所有模型的属性,可以在构建关联关系查询时使用 withAttributes 方法:

php
/**
 * Get the user's featured posts.
 */
public function featuredPosts(): HasMany
{
    return $this->posts()->withAttributes(['featured' => true]);
}

withAttributes 方法将使用给定的属性向查询添加 where 条件,并且还将给定的属性添加到通过关联关系方法创建的任何模型中:

php
$post = $user->featuredPosts()->create(['title' => 'Featured Post']);

$post->featured; // true

要指示 withAttributes 方法不向查询添加 where 条件,可以将 asConditions 参数设置为 false

php
return $this->posts()->withAttributes(['featured' => true], asConditions: false);

多对多关联关系

多对多关联关系比 hasOnehasMany 关联关系稍微复杂一些。多对多关联关系的一个例子是用户拥有许多角色,这些角色也被应用程序中的其他用户共享。例如,一个用户可能被分配了"作者"和"编辑"角色;但是,这些角色也可能被分配给其他用户。因此,一个用户有多个角色,一个角色有多个用户。

表结构

要定义此关联关系,需要三个数据库表:usersrolesrole_userrole_user 表从相关模型名称的字母顺序派生,包含 user_idrole_id 列。此表用作链接用户和角色的中间表。

请记住,由于一个角色可以属于多个用户,我们不能简单地在 roles 表上放置 user_id 列。这意味着一个角色只能属于一个用户。为了支持将角色分配给多个用户,需要 role_user 表。我们可以这样总结关联关系的表结构:

text
users
    id - integer
    name - string

roles
    id - integer
    name - string

role_user
    user_id - integer
    role_id - integer

模型结构

多对多关联关系通过编写一个返回 belongsToMany 方法结果的方法来定义。belongsToMany 方法由 Illuminate\Database\Eloquent\Model 基类提供,所有应用程序的 Eloquent 模型都使用它。例如,让我们在 User 模型上定义一个 roles 方法。传递给此方法的第一个参数是关联模型类的名称:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class User extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function roles(): BelongsToMany
    {
        return $this->belongsToMany(Role::class);
    }
}

一旦定义了关联关系,你可以使用 roles 动态关联属性访问用户的角色:

php
use App\Models\User;

$user = User::find(1);

foreach ($user->roles as $role) {
    // ...
}

由于所有关联关系也充当查询构建器,你可以通过调用 roles 方法并继续在查询上链式添加条件来添加更多约束:

php
$roles = User::find(1)->roles()->orderBy('name')->get();

要确定关联关系中间表的表名,Eloquent 将按字母顺序连接两个关联模型名称。但是,你可以自由覆盖此约定。你可以通过向 belongsToMany 方法传递第二个参数来实现:

php
return $this->belongsToMany(Role::class, 'role_user');

除了自定义中间表的名称外,你还可以通过向 belongsToMany 方法传递额外参数来自定义表上键的列名。第三个参数是你正在定义关联关系的模型的外键名称,第四个参数是你要连接的模型的外键名称:

php
return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id');

定义反向关联关系

要定义多对多关联关系的"反向",你应该在关联模型上定义一个同样返回 belongsToMany 方法结果的方法。为了完成我们的用户/角色示例,让我们在 Role 模型上定义 users 方法:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Role extends Model
{
    /**
     * The users that belong to the role.
     */
    public function users(): BelongsToMany
    {
        return $this->belongsToMany(User::class);
    }
}

如你所见,关联关系的定义与其 User 模型对应部分完全相同,只是引用了 App\Models\User 模型。由于我们重用了 belongsToMany 方法,在定义多对多关联关系的"反向"时,所有常用的表和键自定义选项都可用。

检索中间表列

如你所学,使用多对多关联关系需要一个中间表的存在。Eloquent 提供了一些非常有用的方式来与此表交互。例如,假设我们的 User 模型有许多与之关联的 Role 模型。访问此关联关系后,我们可以使用模型上的 pivot 属性访问中间表:

php
use App\Models\User;

$user = User::find(1);

foreach ($user->roles as $role) {
    echo $role->pivot->created_at;
}

请注意,我们检索的每个 Role 模型都会自动分配一个 pivot 属性。此属性包含一个表示中间表的模型。

默认情况下,pivot 模型上只会存在模型键。如果你的中间表包含额外的属性,你必须在定义关联关系时指定它们:

php
return $this->belongsToMany(Role::class)->withPivot('active', 'created_by');

如果你希望中间表有由 Eloquent 自动维护的 created_atupdated_at 时间戳,请在定义关联关系时调用 withTimestamps 方法:

php
return $this->belongsToMany(Role::class)->withTimestamps();

WARNING

使用 Eloquent 自动维护时间戳的中间表必须同时具有 created_atupdated_at 时间戳列。

自定义 pivot 属性名称

如前所述,中间表的属性可以通过 pivot 属性在模型上访问。但是,你可以自由自定义此属性的名称,以更好地反映其在应用程序中的用途。

例如,如果你的应用程序包含可以订阅播客的用户,你可能在用户和播客之间有多对多关联关系。如果是这种情况,你可能希望将中间表属性重命名为 subscription 而不是 pivot。这可以在定义关联关系时使用 as 方法来完成:

php
return $this->belongsToMany(Podcast::class)
    ->as('subscription')
    ->withTimestamps();

一旦指定了自定义中间表属性,你可以使用自定义名称访问中间表数据:

php
$users = User::with('podcasts')->get();

foreach ($users->flatMap->podcasts as $podcast) {
    echo $podcast->subscription->created_at;
}

通过中间表列过滤查询

你还可以在定义关联关系时使用 wherePivotwherePivotInwherePivotNotInwherePivotBetweenwherePivotNotBetweenwherePivotNullwherePivotNotNull 方法过滤 belongsToMany 关联关系查询返回的结果:

php
return $this->belongsToMany(Role::class)
    ->wherePivot('approved', 1);

return $this->belongsToMany(Role::class)
    ->wherePivotIn('priority', [1, 2]);

return $this->belongsToMany(Role::class)
    ->wherePivotNotIn('priority', [1, 2]);

return $this->belongsToMany(Podcast::class)
    ->as('subscriptions')
    ->wherePivotBetween('created_at', ['2020-01-01 00:00:00', '2020-12-31 00:00:00']);

return $this->belongsToMany(Podcast::class)
    ->as('subscriptions')
    ->wherePivotNotBetween('created_at', ['2020-01-01 00:00:00', '2020-12-31 00:00:00']);

return $this->belongsToMany(Podcast::class)
    ->as('subscriptions')
    ->wherePivotNull('expired_at');

return $this->belongsToMany(Podcast::class)
    ->as('subscriptions')
    ->wherePivotNotNull('expired_at');

wherePivot 会向查询添加 where 子句约束,但在通过定义的关联关系创建新模型时不会添加指定的值。如果你需要同时查询和创建具有特定 pivot 值的关联关系,可以使用 withPivotValue 方法:

php
return $this->belongsToMany(Role::class)
    ->withPivotValue('approved', 1);

通过中间表列排序查询

你可以使用 orderByPivotorderByPivotDesc 方法对 belongsToMany 关联关系查询返回的结果进行排序。在以下示例中,我们将检索用户的所有最新徽章:

php
return $this->belongsToMany(Badge::class)
    ->where('rank', 'gold')
    ->orderByPivotDesc('created_at');

定义自定义中间表模型

如果你想定义一个自定义模型来表示多对多关联关系的中间表,可以在定义关联关系时调用 using 方法。自定义 pivot 模型使你有机会在 pivot 模型上定义额外的行为,例如方法和类型转换。

自定义多对多 pivot 模型应继承 Illuminate\Database\Eloquent\Relations\Pivot 类,而自定义多态多对多 pivot 模型应继承 Illuminate\Database\Eloquent\Relations\MorphPivot 类。例如,我们可以定义一个使用自定义 RoleUser pivot 模型的 Role 模型:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Role extends Model
{
    /**
     * The users that belong to the role.
     */
    public function users(): BelongsToMany
    {
        return $this->belongsToMany(User::class)->using(RoleUser::class);
    }
}

定义 RoleUser 模型时,你应该继承 Illuminate\Database\Eloquent\Relations\Pivot 类:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Relations\Pivot;

class RoleUser extends Pivot
{
    // ...
}

WARNING

Pivot 模型不能使用 SoftDeletes trait。如果你需要软删除 pivot 记录,请考虑将 pivot 模型转换为实际的 Eloquent 模型。

自定义 Pivot 模型和自增 ID

如果你定义了使用自定义 pivot 模型的多对多关联关系,并且该 pivot 模型具有自增主键,你应该确保自定义 pivot 模型类使用 Table 属性并将 incrementing 设置为 true

php
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\Relations\Pivot;

#[Table(incrementing: true)]
class RoleUser extends Pivot
{
    // ...
}

多态关联关系

多态关联关系允许子模型使用单个关联属于多种类型的模型。例如,假设你正在构建一个允许用户分享博客文章和视频的应用程序。在这样的应用程序中,Comment 模型可能同时属于 PostVideo 模型。

一对一(多态)

表结构

一对一多态关联关系类似于典型的一对一关联关系;但是,子模型可以使用单个关联属于多种类型的模型。例如,博客 PostUser 可以与 Image 模型共享多态关联关系。使用一对一多态关联关系允许你拥有一个可能与文章和用户关联的唯一图片的单一表。首先,让我们检查表结构:

text
posts
    id - integer
    name - string

users
    id - integer
    name - string

images
    id - integer
    url - string
    imageable_id - integer
    imageable_type - string

注意 images 表上的 imageable_idimageable_type 列。imageable_id 列将包含文章或用户的 ID 值,而 imageable_type 列将包含父模型的类名。Eloquent 使用 imageable_type 列来确定在访问 imageable 关联关系时返回哪种"类型"的父模型。在这种情况下,该列将包含 App\Models\PostApp\Models\User

模型结构

接下来,让我们检查构建此关联关系所需的模型定义:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Image extends Model
{
    /**
     * Get the parent imageable model (user or post).
     */
    public function imageable(): MorphTo
    {
        return $this->morphTo();
    }
}

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphOne;

class Post extends Model
{
    /**
     * Get the post's image.
     */
    public function image(): MorphOne
    {
        return $this->morphOne(Image::class, 'imageable');
    }
}

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphOne;

class User extends Model
{
    /**
     * Get the user's image.
     */
    public function image(): MorphOne
    {
        return $this->morphOne(Image::class, 'imageable');
    }
}

检索关联关系

一旦定义了数据库表和模型,你可以通过模型访问关联关系。例如,要检索文章的图片,我们可以访问 image 动态关联属性:

php
use App\Models\Post;

$post = Post::find(1);

$image = $post->image;

你可以通过访问执行 morphTo 调用的方法名称来检索多态模型的父模型。在这种情况下,即 Image 模型上的 imageable 方法。因此,我们将作为动态关联属性访问该方法:

php
use App\Models\Image;

$image = Image::find(1);

$imageable = $image->imageable;

Image 模型上的 imageable 关联关系将返回 PostUser 实例,具体取决于哪种类型的模型拥有该图片。

键约定

如有必要,你可以指定多态子模型使用的"id"和"type"列的名称。如果这样做,请确保始终将关联关系的名称作为第一个参数传递给 morphTo 方法。通常,此值应与方法名匹配,因此你可以使用 PHP 的 __FUNCTION__ 常量:

php
/**
 * Get the model that the image belongs to.
 */
public function imageable(): MorphTo
{
    return $this->morphTo(__FUNCTION__, 'imageable_type', 'imageable_id');
}

一对多(多态)

表结构

一对多多态关联关系类似于典型的一对多关联关系;但是,子模型可以使用单个关联属于多种类型的模型。例如,假设你的应用程序的用户可以对文章和视频"评论"。使用多态关联关系,你可以使用单个 comments 表来包含文章和视频的评论。首先,让我们检查构建此关联关系所需的表结构:

text
posts
    id - integer
    title - string
    body - text

videos
    id - integer
    title - string
    url - string

comments
    id - integer
    body - text
    commentable_id - integer
    commentable_type - string

模型结构

接下来,让我们检查构建此关联关系所需的模型定义:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Comment extends Model
{
    /**
     * Get the parent commentable model (post or video).
     */
    public function commentable(): MorphTo
    {
        return $this->morphTo();
    }
}

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class Post extends Model
{
    /**
     * Get all of the post's comments.
     */
    public function comments(): MorphMany
    {
        return $this->morphMany(Comment::class, 'commentable');
    }
}

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class Video extends Model
{
    /**
     * Get all of the video's comments.
     */
    public function comments(): MorphMany
    {
        return $this->morphMany(Comment::class, 'commentable');
    }
}

检索关联关系

一旦定义了数据库表和模型,你可以通过模型的动态关联属性访问关联关系。例如,要访问文章的所有评论,我们可以使用 comments 动态属性:

php
use App\Models\Post;

$post = Post::find(1);

foreach ($post->comments as $comment) {
    // ...
}

你也可以通过访问执行 morphTo 调用的方法名称来检索多态子模型的父模型。在这种情况下,即 Comment 模型上的 commentable 方法。因此,我们将作为动态关联属性访问该方法以访问评论的父模型:

php
use App\Models\Comment;

$comment = Comment::find(1);

$commentable = $comment->commentable;

Comment 模型上的 commentable 关联关系将返回 PostVideo 实例,具体取决于评论的父模型是哪种类型。

在子模型上自动填充父模型

即使使用了 Eloquent 预加载,如果你在遍历子模型时尝试从子模型访问父模型,仍然会出现"N + 1"查询问题:

php
$posts = Post::with('comments')->get();

foreach ($posts as $post) {
    foreach ($post->comments as $comment) {
        echo $comment->commentable->title;
    }
}

在上面的示例中,即使为每个 Post 模型预加载了评论,但 Eloquent 不会自动在每个子 Comment 模型上填充父 Post,因此引入了"N + 1"查询问题。

如果你希望 Eloquent 自动在子模型上填充父模型,你可以在定义 morphMany 关联关系时调用 chaperone 方法:

php
class Post extends Model
{
    /**
     * Get all of the post's comments.
     */
    public function comments(): MorphMany
    {
        return $this->morphMany(Comment::class, 'commentable')->chaperone();
    }
}

或者,如果你想在运行时选择自动父模型填充,可以在预加载关联关系时调用 chaperone 方法:

php
use App\Models\Post;

$posts = Post::with([
    'comments' => fn ($comments) => $comments->chaperone(),
])->get();

One of Many(多态)

有时一个模型可能有许多关联模型,但你想轻松检索关联关系的"最新"或"最旧"的关联模型。例如,一个 User 模型可能关联许多 Image 模型,但你想定义一种便捷的方式来与用户最近上传的图片交互。你可以使用 morphOne 关联关系类型结合 ofMany 方法来实现:

php
/**
 * Get the user's most recent image.
 */
public function latestImage(): MorphOne
{
    return $this->morphOne(Image::class, 'imageable')->latestOfMany();
}

同样,你可以定义一个方法来检索关联关系的"最旧"或第一个关联模型:

php
/**
 * Get the user's oldest image.
 */
public function oldestImage(): MorphOne
{
    return $this->morphOne(Image::class, 'imageable')->oldestOfMany();
}

默认情况下,latestOfManyoldestOfMany 方法将根据模型的主键检索最新或最旧的关联模型,主键必须是可排序的。但有时你可能希望使用不同的排序标准从较大的关联关系中检索单个模型。

例如,使用 ofMany 方法,你可以检索用户"最受欢迎"的图片。ofMany 方法接受可排序列作为其第一个参数,以及查询关联模型时要应用的聚合函数(minmax):

php
/**
 * Get the user's most popular image.
 */
public function bestImage(): MorphOne
{
    return $this->morphOne(Image::class, 'imageable')->ofMany('likes', 'max');
}

NOTE

可以构建更高级的"one of many"关联关系。有关更多信息,请参阅 has one of many 文档

多对多(多态)

表结构

多对多多态关联关系比"morph one"和"morph many"关联关系稍微复杂一些。例如,Post 模型和 Video 模型可以与 Tag 模型共享多态关联关系。在这种情况下使用多对多多态关联关系,将允许你的应用程序拥有一个可能与文章或视频关联的唯一标签的单一表。首先,让我们检查构建此关联关系所需的表结构:

text
posts
    id - integer
    name - string

videos
    id - integer
    name - string

tags
    id - integer
    name - string

taggables
    tag_id - integer
    taggable_id - integer
    taggable_type - string

NOTE

在深入了解多态多对多关联关系之前,你可能会受益于阅读典型多对多关联关系的文档。

模型结构

接下来,我们准备在模型上定义关联关系。PostVideo 模型都将包含一个调用基础 Eloquent 模型类提供的 morphToMany 方法的 tags 方法。

morphToMany 方法接受关联模型的名称以及"关联关系名称"。根据我们分配给中间表名称和它包含的键,我们将关联关系称为"taggable":

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

class Post extends Model
{
    /**
     * Get all of the tags for the post.
     */
    public function tags(): MorphToMany
    {
        return $this->morphToMany(Tag::class, 'taggable');
    }
}

定义反向关联关系

接下来,在 Tag 模型上,你应该为每个可能的父模型定义一个方法。因此,在此示例中,我们将定义 posts 方法和 videos 方法。这两个方法都应返回 morphedByMany 方法的结果。

morphedByMany 方法接受关联模型的名称以及"关联关系名称"。根据我们分配给中间表名称和它包含的键,我们将关联关系称为"taggable":

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

class Tag extends Model
{
    /**
     * Get all of the posts that are assigned this tag.
     */
    public function posts(): MorphToMany
    {
        return $this->morphedByMany(Post::class, 'taggable');
    }

    /**
     * Get all of the videos that are assigned this tag.
     */
    public function videos(): MorphToMany
    {
        return $this->morphedByMany(Video::class, 'taggable');
    }
}

检索关联关系

一旦定义了数据库表和模型,你可以通过模型访问关联关系。例如,要访问文章的所有标签,你可以使用 tags 动态关联属性:

php
use App\Models\Post;

$post = Post::find(1);

foreach ($post->tags as $tag) {
    // ...
}

你可以通过访问执行 morphedByMany 调用的方法名称来检索多态子模型的父模型。在这种情况下,即 Tag 模型上的 postsvideos 方法:

php
use App\Models\Tag;

$tag = Tag::find(1);

foreach ($tag->posts as $post) {
    // ...
}

foreach ($tag->videos as $video) {
    // ...
}

自定义多态类型

默认情况下,Laravel 将使用完全限定的类名来存储关联模型的"类型"。例如,给定上面的一对多关联关系示例,其中 Comment 模型可能属于 PostVideo 模型,默认的 commentable_type 将分别为 App\Models\PostApp\Models\Video。但是,你可能希望将这些值与应用程序的内部结构解耦。

例如,我们可以使用简单的字符串如 postvideo 代替模型名称作为"类型"。这样,即使模型被重命名,数据库中的多态"类型"列值也将保持有效:

php
use Illuminate\Database\Eloquent\Relations\Relation;

Relation::enforceMorphMap([
    'post' => 'App\Models\Post',
    'video' => 'App\Models\Video',
]);

你可以在 App\Providers\AppServiceProvider 类的 boot 方法中调用 enforceMorphMap 方法,或者如果你愿意,可以创建一个单独的服务提供者。

你可以在运行时使用模型的 getMorphClass 方法确定给定模型的 morph 别名。相反,你可以使用 Relation::getMorphedModel 方法确定与 morph 别名关联的完全限定类名:

php
use Illuminate\Database\Eloquent\Relations\Relation;

$alias = $post->getMorphClass();

$class = Relation::getMorphedModel($alias);

WARNING

向现有应用程序添加"morph map"时,数据库中仍然包含完全限定类名的每个可变形 *_type 列值都需要转换为其"map"名称。

动态关联关系

你可以使用 resolveRelationUsing 方法在运行时定义 Eloquent 模型之间的关联关系。虽然通常不建议在正常应用程序开发中使用,但在开发 Laravel 包时可能偶尔会有用。

resolveRelationUsing 方法接受所需的关联关系名称作为其第一个参数。传递给该方法的第二个参数应该是一个闭包,接受模型实例并返回有效的 Eloquent 关联关系定义。通常,你应该在服务提供者的 boot 方法中配置动态关联关系:

php
use App\Models\Order;
use App\Models\Customer;

Order::resolveRelationUsing('customer', function (Order $orderModel) {
    return $orderModel->belongsTo(Customer::class, 'customer_id');
});

WARNING

定义动态关联关系时,始终为 Eloquent 关联关系方法提供显式的键名参数。

查询关联关系

由于所有 Eloquent 关联关系都是通过方法定义的,你可以调用这些方法来获取关联关系的实例,而无需实际执行查询来加载关联模型。此外,所有类型的 Eloquent 关联关系也充当查询构建器,允许你在最终对数据库执行 SQL 查询之前继续在关联关系查询上链式添加约束。

例如,假设一个博客应用程序中 User 模型有许多关联的 Post 模型:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class User extends Model
{
    /**
     * Get all of the posts for the user.
     */
    public function posts(): HasMany
    {
        return $this->hasMany(Post::class);
    }
}

你可以查询 posts 关联关系并向关联关系添加额外约束,如下所示:

php
use App\Models\User;

$user = User::find(1);

$user->posts()->where('active', 1)->get();

你可以在关联关系上使用任何 Laravel 查询构建器的方法,因此请务必浏览查询构建器文档以了解所有可用的方法。

在关联关系后链式调用 orWhere 子句

如上面的示例所示,你可以在查询关联关系时自由地向关联关系添加额外的约束。但是,在关联关系上链式调用 orWhere 子句时要小心,因为 orWhere 子句将在与关联关系约束相同的级别上进行逻辑分组:

php
$user->posts()
    ->where('active', 1)
    ->orWhere('votes', '>=', 100)
    ->get();

上面的示例将生成以下 SQL。如你所见,or 子句指示查询返回投票数大于 100 的 任何 文章。查询不再限于特定用户:

sql
select *
from posts
where user_id = ? and active = 1 or votes >= 100

在大多数情况下,你应该使用逻辑分组将条件检查分组在括号之间:

php
use Illuminate\Database\Eloquent\Builder;

$user->posts()
    ->where(function (Builder $query) {
        return $query->where('active', 1)
            ->orWhere('votes', '>=', 100);
    })
    ->get();

上面的示例将产生以下 SQL。请注意,逻辑分组已正确分组了约束,查询仍然限于特定用户:

sql
select *
from posts
where user_id = ? and (active = 1 or votes >= 100)

关联关系方法 vs. 动态属性

如果你不需要向 Eloquent 关联关系查询添加额外的约束,你可以像访问属性一样访问关联关系。例如,继续使用我们的 UserPost 示例模型,我们可以这样访问用户的所有文章:

php
use App\Models\User;

$user = User::find(1);

foreach ($user->posts as $post) {
    // ...
}

动态关联属性执行"延迟加载",这意味着它们只会在你实际访问时加载关联关系数据。因此,开发者经常使用预加载来预先加载他们知道在加载模型后将被访问的关联关系。预加载显著减少了加载模型关联关系时必须执行的 SQL 查询。

查询关联关系是否存在

检索模型记录时,你可能希望根据关联关系的存在来限制结果。例如,假设你想检索所有至少有一条评论的博客文章。为此,你可以将关联关系的名称传递给 hasorHas 方法:

php
use App\Models\Post;

// 检索所有至少有一条评论的文章...
$posts = Post::has('comments')->get();

你还可以指定运算符和计数值来进一步自定义查询:

php
// 检索所有有三条或更多评论的文章...
$posts = Post::has('comments', '>=', 3)->get();

可以使用"点"语法构建嵌套的 has 语句。例如,你可以检索所有至少有一条评论且该评论至少有一张图片的文章:

php
// 检索至少有一条带图片的评论的文章...
$posts = Post::has('comments.images')->get();

如果你需要更强大的功能,可以使用 whereHasorWhereHas 方法在 has 查询上定义额外的查询约束,例如检查评论的内容:

php
use Illuminate\Database\Eloquent\Builder;

// 检索至少有一条包含类似 code% 词语的评论的文章...
$posts = Post::whereHas('comments', function (Builder $query) {
    $query->where('content', 'like', 'code%');
})->get();

// 检索至少有十条包含类似 code% 词语的评论的文章...
$posts = Post::whereHas('comments', function (Builder $query) {
    $query->where('content', 'like', 'code%');
}, '>=', 10)->get();

WARNING

Eloquent 目前不支持跨数据库查询关联关系是否存在。关联关系必须存在于同一数据库中。

多对多关联关系存在查询

whereAttachedTo 方法可用于查询与某个模型或模型集合具有多对多附件关系的模型:

php
$users = User::whereAttachedTo($role)->get();

你也可以向 whereAttachedTo 方法提供集合实例。这样做时,Laravel 将检索附加到集合中任何模型的模型:

php
$tags = Tag::whereLike('name', '%laravel%')->get();

$posts = Post::whereAttachedTo($tags)->get();

内联关联关系存在查询

如果你想使用附加到关联关系查询的单个简单 where 条件来查询关联关系的存在,你可能会发现使用 whereRelationorWhereRelationwhereMorphRelationorWhereMorphRelation 方法更方便。例如,我们可以查询所有有未批准评论的文章:

php
use App\Models\Post;

$posts = Post::whereRelation('comments', 'is_approved', false)->get();

当然,与查询构建器的 where 方法调用一样,你也可以指定运算符:

php
$posts = Post::whereRelation(
    'comments', 'created_at', '>=', now()->minus(hours: 1)
)->get();

查询关联关系不存在

检索模型记录时,你可能希望根据关联关系的不存在来限制结果。例如,假设你想检索所有没有任何评论的博客文章。为此,你可以将关联关系的名称传递给 doesntHaveorDoesntHave 方法:

php
use App\Models\Post;

$posts = Post::doesntHave('comments')->get();

如果你需要更强大的功能,可以使用 whereDoesntHaveorWhereDoesntHave 方法向 doesntHave 查询添加额外的查询约束,例如检查评论的内容:

php
use Illuminate\Database\Eloquent\Builder;

$posts = Post::whereDoesntHave('comments', function (Builder $query) {
    $query->where('content', 'like', 'code%');
})->get();

你可以使用"点"语法对嵌套关联关系执行查询。例如,以下查询将检索所有没有评论的文章以及有评论但没有任何来自被封禁用户的评论的文章:

php
use Illuminate\Database\Eloquent\Builder;

$posts = Post::whereDoesntHave('comments.author', function (Builder $query) {
    $query->where('banned', 1);
})->get();

查询 Morph To 关联关系

要查询"morph to"关联关系的存在,你可以使用 whereHasMorphwhereDoesntHaveMorph 方法。这些方法接受关联关系的名称作为其第一个参数。接下来,这些方法接受你希望包含在查询中的关联模型的名称。最后,你可以提供一个自定义关联关系查询的闭包:

php
use App\Models\Comment;
use App\Models\Post;
use App\Models\Video;
use Illuminate\Database\Eloquent\Builder;

// 检索与标题类似 code% 的文章或视频关联的评论...
$comments = Comment::whereHasMorph(
    'commentable',
    [Post::class, Video::class],
    function (Builder $query) {
        $query->where('title', 'like', 'code%');
    }
)->get();

// 检索与标题不类似 code% 的文章关联的评论...
$comments = Comment::whereDoesntHaveMorph(
    'commentable',
    Post::class,
    function (Builder $query) {
        $query->where('title', 'like', 'code%');
    }
)->get();

你可能偶尔需要根据关联多态模型的"类型"添加查询约束。传递给 whereHasMorph 方法的闭包可以接收 $type 值作为其第二个参数。此参数允许你检查正在构建的查询的"类型":

php
use Illuminate\Database\Eloquent\Builder;

$comments = Comment::whereHasMorph(
    'commentable',
    [Post::class, Video::class],
    function (Builder $query, string $type) {
        $column = $type === Post::class ? 'content' : 'title';

        $query->where($column, 'like', 'code%');
    }
)->get();

有时你可能想查询"morph to"关联关系的父模型的子模型。你可以使用 whereMorphedTowhereNotMorphedTo 方法来实现,它们将自动确定给定模型的正确 morph 类型映射。这些方法接受 morphTo 关联关系的名称作为其第一个参数,关联的父模型作为其第二个参数:

php
$comments = Comment::whereMorphedTo('commentable', $post)
    ->orWhereMorphedTo('commentable', $video)
    ->get();

查询所有关联模型

你可以提供 * 作为通配符值,而不是传递可能的多态模型数组。这将指示 Laravel 从数据库中检索所有可能的多态类型。Laravel 将执行额外的查询来执行此操作:

php
use Illuminate\Database\Eloquent\Builder;

$comments = Comment::whereHasMorph('commentable', '*', function (Builder $query) {
    $query->where('title', 'like', 'foo%');
})->get();

有时你可能想计算给定关联关系的关联模型数量而不实际加载模型。为此,你可以使用 withCount 方法。withCount 方法将在结果模型上放置一个 {relation}_count 属性:

php
use App\Models\Post;

$posts = Post::withCount('comments')->get();

foreach ($posts as $post) {
    echo $post->comments_count;
}

通过向 withCount 方法传递数组,你可以为多个关联关系添加"计数",并向查询添加额外的约束:

php
use Illuminate\Database\Eloquent\Builder;

$posts = Post::withCount(['votes', 'comments' => function (Builder $query) {
    $query->where('content', 'like', 'code%');
}])->get();

echo $posts[0]->votes_count;
echo $posts[0]->comments_count;

你还可以为关联关系计数结果设置别名,允许对同一关联关系进行多次计数:

php
use Illuminate\Database\Eloquent\Builder;

$posts = Post::withCount([
    'comments',
    'comments as pending_comments_count' => function (Builder $query) {
        $query->where('approved', false);
    },
])->get();

echo $posts[0]->comments_count;
echo $posts[0]->pending_comments_count;

延迟计数加载

使用 loadCount 方法,你可以在父模型已经被检索之后加载关联关系计数:

php
$book = Book::first();

$book->loadCount('genres');

如果你需要在计数查询上设置额外的查询约束,你可以传递一个以你想要计数的关联关系为键的数组。数组值应该是接收查询构建器实例的闭包:

php
$book->loadCount(['reviews' => function (Builder $query) {
    $query->where('rating', 5);
}])

关联关系计数和自定义 Select 语句

如果你将 withCountselect 语句组合使用,请确保在 select 方法之后调用 withCount

php
$posts = Post::select(['title', 'body'])
    ->withCount('comments')
    ->get();

其他聚合函数

除了 withCount 方法外,Eloquent 还提供了 withMinwithMaxwithAvgwithSumwithExists 方法。这些方法将在结果模型上放置一个 {relation}_{function}_{column} 属性:

php
use App\Models\Post;

$posts = Post::withSum('comments', 'votes')->get();

foreach ($posts as $post) {
    echo $post->comments_sum_votes;
}

如果你希望使用另一个名称访问聚合函数的结果,可以指定自己的别名:

php
$posts = Post::withSum('comments as total_comments', 'votes')->get();

foreach ($posts as $post) {
    echo $post->total_comments;
}

loadCount 方法一样,这些方法的延迟版本也可用。这些额外的聚合操作可以在已经检索到的 Eloquent 模型上执行:

php
$post = Post::first();

$post->loadSum('comments', 'votes');

如果你将这些聚合方法与 select 语句组合使用,请确保在 select 方法之后调用聚合方法:

php
$posts = Post::select(['title', 'body'])
    ->withExists('comments')
    ->get();

如果你想预加载一个"morph to"关联关系,以及该关联关系可能返回的各种实体的关联模型计数,你可以使用 with 方法结合 morphTo 关联关系的 morphWithCount 方法。

在此示例中,假设 PhotoPost 模型可以创建 ActivityFeed 模型。我们假设 ActivityFeed 模型定义了一个名为 parentable 的"morph to"关联关系,允许我们检索给定 ActivityFeed 实例的父 PhotoPost 模型。此外,假设 Photo 模型"有多个" Tag 模型,Post 模型"有多个" Comment 模型。

现在,假设我们想检索 ActivityFeed 实例并预加载每个 ActivityFeed 实例的 parentable 父模型。此外,我们想检索与每个父照片关联的标签数量以及与每个父文章关联的评论数量:

php
use Illuminate\Database\Eloquent\Relations\MorphTo;

$activities = ActivityFeed::with([
    'parentable' => function (MorphTo $morphTo) {
        $morphTo->morphWithCount([
            Photo::class => ['tags'],
            Post::class => ['comments'],
        ]);
    }])->get();

延迟计数加载

假设我们已经检索了一组 ActivityFeed 模型,现在想加载与活动动态关联的各种 parentable 模型的嵌套关联关系计数。你可以使用 loadMorphCount 方法来实现:

php
$activities = ActivityFeed::with('parentable')->get();

$activities->loadMorphCount('parentable', [
    Photo::class => ['tags'],
    Post::class => ['comments'],
]);

预加载

当将 Eloquent 关联关系作为属性访问时,关联模型是"延迟加载"的。这意味着关联关系数据在你首次访问属性之前实际上不会被加载。但是,Eloquent 可以在查询父模型时"预加载"关联关系。预加载缓解了"N + 1"查询问题。为说明 N + 1 查询问题,考虑一个"属于"一个 Author 模型的 Book 模型:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Book extends Model
{
    /**
     * Get the author that wrote the book.
     */
    public function author(): BelongsTo
    {
        return $this->belongsTo(Author::class);
    }
}

现在,让我们检索所有书籍及其作者:

php
use App\Models\Book;

$books = Book::all();

foreach ($books as $book) {
    echo $book->author->name;
}

此循环将执行一个查询来检索数据库表中的所有书籍,然后为每本书执行另一个查询来检索书籍的作者。因此,如果我们有 25 本书,上面的代码将运行 26 个查询:一个用于原始书籍查询,另外 25 个查询用于检索每本书的作者。

幸运的是,我们可以使用预加载将此操作减少到只有两个查询。构建查询时,你可以使用 with 方法指定应预加载哪些关联关系:

php
$books = Book::with('author')->get();

foreach ($books as $book) {
    echo $book->author->name;
}

对于此操作,只会执行两个查询 - 一个查询检索所有书籍,一个查询检索所有书籍的所有作者:

sql
select * from books

select * from authors where id in (1, 2, 3, 4, 5, ...)

预加载多个关联关系

有时你可能需要预加载多个不同的关联关系。为此,只需将关联关系数组传递给 with 方法:

php
$books = Book::with(['author', 'publisher'])->get();

嵌套预加载

要预加载关联关系的关联关系,你可以使用"点"语法。例如,让我们预加载所有书籍的作者以及所有作者的个人联系方式:

php
$books = Book::with('author.contacts')->get();

或者,你可以通过向 with 方法提供嵌套数组来指定嵌套预加载关联关系,这在预加载多个嵌套关联关系时很方便:

php
$books = Book::with([
    'author' => [
        'contacts',
        'publisher',
    ],
])->get();

嵌套预加载 morphTo 关联关系

如果你想预加载一个 morphTo 关联关系,以及该关联关系可能返回的各种实体上的嵌套关联关系,你可以使用 with 方法结合 morphTo 关联关系的 morphWith 方法。为了帮助说明此方法,让我们考虑以下模型:

php
<?php

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class ActivityFeed extends Model
{
    /**
     * Get the parent of the activity feed record.
     */
    public function parentable(): MorphTo
    {
        return $this->morphTo();
    }
}

在此示例中,假设 EventPhotoPost 模型可以创建 ActivityFeed 模型。此外,假设 Event 模型属于一个 Calendar 模型,Photo 模型与 Tag 模型关联,Post 模型属于一个 Author 模型。

使用这些模型定义和关联关系,我们可以检索 ActivityFeed 模型实例并预加载所有 parentable 模型及其各自的嵌套关联关系:

php
use Illuminate\Database\Eloquent\Relations\MorphTo;

$activities = ActivityFeed::query()
    ->with(['parentable' => function (MorphTo $morphTo) {
        $morphTo->morphWith([
            Event::class => ['calendar'],
            Photo::class => ['tags'],
            Post::class => ['author'],
        ]);
    }])->get();

预加载特定列

你可能并不总是需要检索的关联关系中的每一列。因此,Eloquent 允许你指定要检索关联关系的哪些列:

php
$books = Book::with('author:id,name,book_id')->get();

WARNING

使用此功能时,你应始终在要检索的列列表中包含 id 列和任何相关的外键列。

默认预加载

有时你可能希望在检索模型时始终加载某些关联关系。为此,你可以在模型上定义 $with 属性:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Book extends Model
{
    /**
     * The relationships that should always be loaded.
     *
     * @var array
     */
    protected $with = ['author'];

    /**
     * Get the author that wrote the book.
     */
    public function author(): BelongsTo
    {
        return $this->belongsTo(Author::class);
    }

    /**
     * Get the genre of the book.
     */
    public function genre(): BelongsTo
    {
        return $this->belongsTo(Genre::class);
    }
}

如果你想在单个查询中从 $with 属性中移除一项,可以使用 without 方法:

php
$books = Book::without('author')->get();

如果你想在单个查询中覆盖 $with 属性中的所有项,可以使用 withOnly 方法:

php
$books = Book::withOnly('genre')->get();

约束预加载

有时你可能希望预加载一个关联关系,但还要为预加载查询指定额外的查询条件。你可以通过将关联关系数组传递给 with 方法来实现,其中数组键是关联关系名称,数组值是向预加载查询添加额外约束的闭包:

php
use App\Models\User;

$users = User::with(['posts' => function ($query) {
    $query->where('title', 'like', '%code%');
}])->get();

在此示例中,Eloquent 将只预加载文章 title 列包含单词 code 的文章。你可以调用其他查询构建器方法来进一步自定义预加载操作:

php
$users = User::with(['posts' => function ($query) {
    $query->orderBy('created_at', 'desc');
}])->get();

约束 morphTo 关联关系的预加载

如果你正在预加载一个 morphTo 关联关系,Eloquent 将运行多个查询来获取每种类型的关联模型。你可以使用 MorphTo 关联关系的 constrain 方法向这些查询中的每一个添加额外约束:

php
use Illuminate\Database\Eloquent\Relations\MorphTo;

$comments = Comment::with(['commentable' => function (MorphTo $morphTo) {
    $morphTo->constrain([
        Post::class => function ($query) {
            $query->whereNull('hidden_at');
        },
        Video::class => function ($query) {
            $query->where('type', 'educational');
        },
    ]);
}])->get();

在此示例中,Eloquent 将只预加载未被隐藏的文章和 type 值为"educational"的视频。

通过关联关系存在约束预加载

有时你可能发现自己需要检查关联关系是否存在,同时根据相同的条件加载关联关系。例如,你可能希望只检索具有匹配给定查询条件的子 Post 模型的 User 模型,同时也预加载匹配的文章。你可以使用 withWhereHas 方法来实现:

php
use App\Models\User;

$users = User::withWhereHas('posts', function ($query) {
    $query->where('featured', true);
})->get();

延迟预加载

有时你可能需要在父模型已经被检索之后预加载关联关系。例如,如果你需要动态决定是否加载关联模型,这可能很有用:

php
use App\Models\Book;

$books = Book::all();

if ($condition) {
    $books->load('author', 'publisher');
}

如果你需要在预加载查询上设置额外的查询约束,你可以传递一个以你想要加载的关联关系为键的数组。数组值应该是接收查询实例的闭包实例:

php
$author->load(['books' => function ($query) {
    $query->orderBy('published_date', 'asc');
}]);

要仅在尚未加载的情况下加载关联关系,请使用 loadMissing 方法:

php
$book->loadMissing('author');

嵌套延迟预加载和 morphTo

如果你想预加载一个 morphTo 关联关系,以及该关联关系可能返回的各种实体上的嵌套关联关系,你可以使用 loadMorph 方法。

此方法接受 morphTo 关联关系的名称作为其第一个参数,以及模型/关联关系对数组作为其第二个参数。为了帮助说明此方法,让我们考虑以下模型:

php
<?php

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class ActivityFeed extends Model
{
    /**
     * Get the parent of the activity feed record.
     */
    public function parentable(): MorphTo
    {
        return $this->morphTo();
    }
}

在此示例中,假设 EventPhotoPost 模型可以创建 ActivityFeed 模型。此外,假设 Event 模型属于一个 Calendar 模型,Photo 模型与 Tag 模型关联,Post 模型属于一个 Author 模型。

使用这些模型定义和关联关系,我们可以检索 ActivityFeed 模型实例并预加载所有 parentable 模型及其各自的嵌套关联关系:

php
$activities = ActivityFeed::with('parentable')
    ->get()
    ->loadMorph('parentable', [
        Event::class => ['calendar'],
        Photo::class => ['tags'],
        Post::class => ['author'],
    ]);

自动预加载

WARNING

此功能目前处于测试阶段,以收集社区反馈。此功能的行为和功能可能会在补丁版本中发生变化。

在许多情况下,Laravel 可以自动预加载你访问的关联关系。要启用自动预加载,你应该在应用程序的 AppServiceProviderboot 方法中调用 Model::automaticallyEagerLoadRelationships 方法:

php
use Illuminate\Database\Eloquent\Model;

/**
 * Bootstrap any application services.
 */
public function boot(): void
{
    Model::automaticallyEagerLoadRelationships();
}

启用此功能后,Laravel 将尝试自动加载你访问的任何之前未加载的关联关系。例如,考虑以下场景:

php
use App\Models\User;

$users = User::all();

foreach ($users as $user) {
    foreach ($user->posts as $post) {
        foreach ($post->comments as $comment) {
            echo $comment->content;
        }
    }
}

通常,上面的代码会为每个用户执行一个查询来检索他们的文章,以及为每篇文章执行一个查询来检索其评论。但是,当 automaticallyEagerLoadRelationships 功能启用后,当你尝试访问任何已检索用户的文章时,Laravel 将自动为用户集合中的所有用户延迟预加载文章。同样,当你尝试访问任何已检索文章的评论时,所有评论将为最初检索的所有文章延迟预加载。

如果你不想全局启用自动预加载,你仍然可以通过在集合上调用 withRelationshipAutoloading 方法为单个 Eloquent 集合实例启用此功能:

php
$users = User::where('vip', true)->get();

return $users->withRelationshipAutoloading();

阻止延迟加载

如前所述,预加载关联关系通常可以为你的应用程序提供显著的性能优势。因此,如果你愿意,可以指示 Laravel 始终阻止关联关系的延迟加载。为此,你可以调用基础 Eloquent 模型类提供的 preventLazyLoading 方法。通常,你应该在应用程序的 AppServiceProvider 类的 boot 方法中调用此方法。

preventLazyLoading 方法接受一个可选的布尔参数,指示是否应阻止延迟加载。例如,你可能只想在非生产环境中禁用延迟加载,这样即使生产代码中意外存在延迟加载的关联关系,你的生产环境也能继续正常运行:

php
use Illuminate\Database\Eloquent\Model;

/**
 * Bootstrap any application services.
 */
public function boot(): void
{
    Model::preventLazyLoading(! $this->app->isProduction());
}

阻止延迟加载后,当你的应用程序尝试延迟加载任何 Eloquent 关联关系时,Eloquent 将抛出 Illuminate\Database\LazyLoadingViolationException 异常。

你可以使用 handleLazyLoadingViolationsUsing 方法自定义延迟加载违规的行为。例如,使用此方法,你可以指示延迟加载违规只记录日志而不是通过异常中断应用程序的执行:

php
Model::handleLazyLoadingViolationUsing(function (Model $model, string $relation) {
    $class = $model::class;

    info("Attempted to lazy load [{$relation}] on model [{$class}].");
});

save 方法

Eloquent 提供了向关联关系添加新模型的便捷方法。例如,也许你需要向文章添加新评论。你不必手动设置 Comment 模型上的 post_id 属性,而是可以使用关联关系的 save 方法插入评论:

php
use App\Models\Comment;
use App\Models\Post;

$comment = new Comment(['message' => 'A new comment.']);

$post = Post::find(1);

$post->comments()->save($comment);

请注意,我们没有以动态属性的方式访问 comments 关联关系。相反,我们调用了 comments 方法来获取关联关系的实例。save 方法会自动将适当的 post_id 值添加到新的 Comment 模型中。

如果你需要保存多个关联模型,可以使用 saveMany 方法:

php
$post = Post::find(1);

$post->comments()->saveMany([
    new Comment(['message' => 'A new comment.']),
    new Comment(['message' => 'Another new comment.']),
]);

savesaveMany 方法将持久化给定的模型实例,但不会将新持久化的模型添加到已加载到父模型上的任何内存中的关联关系。如果你计划在使用 savesaveMany 方法后访问关联关系,你可能希望使用 refresh 方法重新加载模型及其关联关系:

php
$post->comments()->save($comment);

$post->refresh();

// 所有评论,包括新保存的评论...
$post->comments;

递归保存模型和关联关系

如果你想 save 你的模型及其所有关联的关联关系,可以使用 push 方法。在此示例中,Post 模型将被保存,以及它的评论和评论的作者:

php
$post = Post::find(1);

$post->comments[0]->message = 'Message';
$post->comments[0]->author->name = 'Author Name';

$post->push();

pushQuietly 方法可用于保存模型及其关联的关联关系而不触发任何事件:

php
$post->pushQuietly();

create 方法

除了 savesaveMany 方法外,你还可以使用 create 方法,它接受一个属性数组,创建模型并将其插入数据库。savecreate 的区别在于 save 接受一个完整的 Eloquent 模型实例,而 create 接受一个普通的 PHP array。新创建的模型将由 create 方法返回:

php
use App\Models\Post;

$post = Post::find(1);

$comment = $post->comments()->create([
    'message' => 'A new comment.',
]);

你可以使用 createMany 方法创建多个关联模型:

php
$post = Post::find(1);

$post->comments()->createMany([
    ['message' => 'A new comment.'],
    ['message' => 'Another new comment.'],
]);

createQuietlycreateManyQuietly 方法可用于创建模型而不分派任何事件:

php
$user = User::find(1);

$user->posts()->createQuietly([
    'title' => 'Post title.',
]);

$user->posts()->createManyQuietly([
    ['title' => 'First post.'],
    ['title' => 'Second post.'],
]);

你还可以使用 findOrNewfirstOrNewfirstOrCreateupdateOrCreate 方法在关联关系上创建和更新模型

NOTE

在使用 create 方法之前,请务必查看批量赋值文档。

Belongs To 关联关系

如果你想将子模型分配给新的父模型,可以使用 associate 方法。在此示例中,User 模型定义了到 Account 模型的 belongsTo 关联关系。此 associate 方法将在子模型上设置外键:

php
use App\Models\Account;

$account = Account::find(10);

$user->account()->associate($account);

$user->save();

要从子模型中移除父模型,可以使用 dissociate 方法。此方法将关联关系的外键设置为 null

php
$user->account()->dissociate();

$user->save();

多对多关联关系

附加 / 分离

Eloquent 还提供了使多对多关联关系的工作更加便捷的方法。例如,假设一个用户可以有多个角色,一个角色可以有多个用户。你可以使用 attach 方法通过在关联关系的中间表中插入记录来将角色附加到用户:

php
use App\Models\User;

$user = User::find(1);

$user->roles()->attach($roleId);

将关联关系附加到模型时,你还可以传递一个要插入到中间表中的额外数据数组:

php
$user->roles()->attach($roleId, ['expires' => $expires]);

有时可能需要从用户中移除一个角色。要移除多对多关联关系记录,请使用 detach 方法。detach 方法将从中间表中删除相应的记录;但是,两个模型都将保留在数据库中:

php
// 从用户中分离单个角色...
$user->roles()->detach($roleId);

// 从用户中分离所有角色...
$user->roles()->detach();

为方便起见,attachdetach 也接受 ID 数组作为输入:

php
$user = User::find(1);

$user->roles()->detach([1, 2, 3]);

$user->roles()->attach([
    1 => ['expires' => $expires],
    2 => ['expires' => $expires],
]);

同步关联

你也可以使用 sync 方法来构建多对多关联。sync 方法接受一个要放置在中间表上的 ID 数组。不在给定数组中的任何 ID 将从中间表中移除。因此,此操作完成后,只有给定数组中的 ID 将存在于中间表中:

php
$user->roles()->sync([1, 2, 3]);

你也可以将额外的中间表值与 ID 一起传递:

php
$user->roles()->sync([1 => ['expires' => true], 2, 3]);

如果你想为每个同步的模型 ID 插入相同的中间表值,可以使用 syncWithPivotValues 方法:

php
$user->roles()->syncWithPivotValues([1, 2, 3], ['active' => true]);

如果你不想分离给定数组中缺少的现有 ID,可以使用 syncWithoutDetaching 方法:

php
$user->roles()->syncWithoutDetaching([1, 2, 3]);

切换关联

多对多关联关系还提供了一个 toggle 方法,用于"切换"给定关联模型 ID 的附加状态。如果给定 ID 当前已附加,则将被分离。同样,如果当前已分离,则将被附加:

php
$user->roles()->toggle([1, 2, 3]);

你也可以将额外的中间表值与 ID 一起传递:

php
$user->roles()->toggle([
    1 => ['expires' => true],
    2 => ['expires' => true],
]);

更新中间表上的记录

如果你需要更新关联关系中间表中的现有行,可以使用 updateExistingPivot 方法。此方法接受中间记录外键和要更新的属性数组:

php
$user = User::find(1);

$user->roles()->updateExistingPivot($roleId, [
    'active' => false,
]);

触碰父时间戳

当模型定义了到另一个模型的 belongsTobelongsToMany 关联关系时(例如 Comment 属于 Post),在子模型更新时更新父模型的时间戳有时会很有帮助。

例如,当 Comment 模型更新时,你可能希望自动"触碰"拥有它的 Postupdated_at 时间戳,使其设置为当前日期和时间。为此,你可以在子模型上使用 Touches 属性,包含当子模型更新时应更新其 updated_at 时间戳的关联关系名称:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Attributes\Touches;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

#[Touches(['post'])]
class Comment extends Model
{
    /**
     * Get the post that the comment belongs to.
     */
    public function post(): BelongsTo
    {
        return $this->belongsTo(Post::class);
    }
}

WARNING

父模型时间戳只会在使用 Eloquent 的 save 方法更新子模型时才会更新。