文档没有参照5.8进行翻译
相关信息:
- 类型:文档文章
- 文章: 模型关联
- 文档: 《Laravel 6 中文文档(6.x)》
此投稿已在 6年前 合并。
内容修改:
| Old | New | Differences |
|---|---|---|
| 1 | ||
| 2 | 1 | # Eloquent: 关联 |
| 3 | 2 | |
| 4 | 3 | - [简介](#introduction) | … | … |
| 16 | 15 | - [多对多](#many-to-many-polymorphic-relations) |
| 17 | 16 | - [自定义多态模型](#custom-polymorphic-types) |
| 18 | 17 | - [查询关联](#querying-relations) |
| 19 | - [关联方法 Vs. 动态 | |
| 20 | - [基于存在的关联 | |
| 21 | - [基于不存在的关联 | |
| 22 | - [多态 | |
| 18 | - [关联方法 Vs. 动态属性](#relationship-methods-vs-dynamic-properties) | |
| 19 | - [基于存在的关联查询](#querying-relationship-existence) | |
| 20 | - [基于不存在的关联查询](#querying-relationship-absence) | |
| 21 | - [多态的关联查询](#querying-polymorphic-relationships) | |
| 23 | 22 | - [关联数据计数](#counting-related-models) |
| 24 | 23 | - [预加载](#eager-loading) |
| 25 | - [ | |
| 24 | - [为预加载添加约束](#constraining-eager-loads) | |
| 26 | 25 | - [延迟预加载](#lazy-eager-loading) |
| 27 | 26 | - [插入 & 更新关联模型](#inserting-and-updating-related-models) |
| 28 | 27 | - [ `save` 方法](#the-save-method) |
| 29 | 28 | - [ `create` 方法](#the-create-method) |
| 30 | - [更新 | |
| 31 | - [ | |
| 29 | - [更新 `Belongs To` 关联](#updating-belongs-to-relationships) | |
| 30 | - [多对多关联](#updating-many-to-many-relationships) | |
| 32 | 31 | - [更新父集时间戳](#touching-parent-timestamps) |
| 33 | 32 | |
| 34 | 33 | <a name="introduction"></a> |
| 35 | 34 | ## 简介 |
| 36 | 35 | |
| 37 | 数据库表通常相互关联。例如,一篇博客可能有很多条评论,或者一个订单可以关联一个下单用户。Eloquent 使这些管理和协作变得简单,并支持多种不同类型的关联: | |
| 38 | ||
| 36 | 数据库表通常相互关联。例如,一篇博客文章可能有很多评论,或者一个订单对应一个下单用户。 Eloquent 让这些关联的管理和使用变得简单,并支持多种类型的关联: | |
| 37 | ||
| 38 | <div class="content-list" markdown="1"> | |
| 39 | 39 | - [一对一](#one-to-one) |
| 40 | 40 | - [一对多](#one-to-many) |
| 41 | 41 | - [多对多](#many-to-many) | … | … |
| 44 | 44 | - [一对一 (多态关联)](#one-to-one-polymorphic-relations) |
| 45 | 45 | - [一对多 (多态关联)](#one-to-many-polymorphic-relations) |
| 46 | 46 | - [多对多 (多态关联)](#many-to-many-polymorphic-relations) |
| 47 | </div> | |
| 47 | 48 | |
| 48 | 49 | <a name="defining-relationships"></a> |
| 49 | 50 | ## 定义关联 |
| 50 | 51 | |
| 51 | Eloquent | |
| 52 | Eloquent 关联在 Eloquent 模型类中以方法的形式呈现。如同 Eloquent 模型本身,关联也可以作为强大的 [查询语句构造器](/docs/{{version}}/queries) 使用,提供了强大的链式调用和查询功能。例如,我们可以在 `posts` 关联的链式调用中附加一个约束条件: | |
| 52 | 53 | |
| 53 | 54 | $user->posts()->where('active', 1)->get(); |
| 54 | 55 | |
| 55 | 不过在深入使用关联之前,让我们先学习如何定义每种关联类型 | |
| 56 | 不过在深入使用关联之前,让我们先学习如何定义每种关联类型。 | |
| 56 | 57 | |
| 57 | 58 | <a name="one-to-one"></a> |
| 58 | 59 | ### 一对一 |
| 59 | 60 | |
| 60 | ||
| 61 | 一对一是最基本的关联关系。例如,一个 `User` 模型可能关联一个 `Phone` 模型。为了定义这个关联,我们要在 `User` 模型中写一个 `phone` 方法。在 `phone` 方法内部调用 `hasOne` 方法并返回其结果: | |
| 61 | 62 | |
| 62 | 63 | <?php |
| 63 | 64 | … | … |
| 68 | 69 | class User extends Model |
| 69 | 70 | { |
| 70 | 71 | /** |
| 71 | * 获 | |
| 72 | * 获取与用户关联的电话记录。 | |
| 72 | 73 | */ |
| 73 | 74 | public function phone() |
| 74 | 75 | { | … | … |
| 76 | 77 | } |
| 77 | 78 | } |
| 78 | 79 | |
| 79 | `hasOne`方法的第一个参数是关联模型的类名。一旦定义了模型关联,我们就可以使用 Eloquent 动态属性获得相关的记录。动态属性允许你访问关系方法就像访问模型中定义的属性一样: | |
| 80 | ||
| 81 | `hasOne` 方法的第一个参数是关联模型的类名。一旦定义了模型关联,我们就可以使用 Eloquent 动态属性获得相关的记录。动态属性允许你访问关系方法就像访问模型中定义的属性一样: | |
| 80 | 82 | |
| 81 | 83 | $phone = User::find(1)->phone; |
| 82 | 84 | |
| 83 | Eloquent 会基于模型名决定外键名称。在这种情况下,会自动假设 | |
| 85 | Eloquent 会基于模型名决定外键名称。在这种情况下,会自动假设 `Phone` 模型有一个 `user_id` 外键。如果你想覆盖这个约定,可以传递第二个参数给 `hasOne` 方法: | |
| 84 | 86 | |
| 85 | 87 | return $this->hasOne('App\Phone', 'foreign_key'); |
| 86 | 88 | |
| 87 | 另外,Eloquent 假设外键的值是与父级 `id` (或自定义 | |
| 89 | 另外,Eloquent 假设外键的值是与父级 `id` (或自定义 `$primaryKey`) 列的值相匹配的。换句话说,Eloquent 将会在 `Phone` 记录的 `user_id` 列中查找与用户表的 `id` 列相匹配的值。如果您希望该关联使用 `id` 以外的自定义键名,则可以给 `hasOne` 方法传递第三个参数: | |
| 88 | 90 | |
| 89 | 91 | return $this->hasOne('App\Phone', 'foreign_key', 'local_key'); |
| 90 | 92 | |
| 91 | 93 | #### 定义反向关联 |
| 92 | 94 | |
| 93 | 我们已经能从 | |
| 95 | 我们已经能从 `User` 模型访问到 `Phone` 模型了。现在,让我们再在 `Phone` 模型上定义一个关联,这个关联能让我们访问到拥有该电话的 `User` 模型。我们可以使用与 `hasOne` 方法对应的 `belongsTo` 方法来定义反向关联: | |
| 94 | 96 | |
| 95 | 97 | <?php |
| 96 | 98 | … | … |
| 101 | 103 | class Phone extends Model |
| 102 | 104 | { |
| 103 | 105 | /** |
| 104 | * 获得拥有此电话的用户 | |
| 106 | * 获得拥有此电话的用户。 | |
| 105 | 107 | */ |
| 106 | 108 | public function user() |
| 107 | 109 | { | … | … |
| 109 | 111 | } |
| 110 | 112 | } |
| 111 | 113 | |
| 112 | 在上面的例子中, Eloquent 会尝试匹配 `Phone` 模型上的 `user_id` 至 `User` 模型上的 `id` 。它是通过检查关系方法的名称并使用` _id` 作为后缀名来确定默认外键名称的。但是,如果 `Phone` 模型的外键不是 `user_id `,那么可以将自定义键名作为第二个参数传递给 `belongsTo` 方法: | |
| 114 | 在上面的例子中, Eloquent 会尝试匹配 `Phone` 模型上的 `user_id` 至 `User` 模型上的 `id` 。它是通过检查关系方法的名称并使用 `_id` 作为后缀名来确定默认外键名称的。但是,如果 `Phone` 模型的外键不是 `user_id`,那么可以将自定义键名作为第二个参数传递给 `belongsTo` 方法: | |
| 115 | ||
| 116 | /** | |
| 117 | * 获得拥有此电话的用户。 | |
| 118 | */ | |
| 119 | public function user() | |
| 120 | { | |
| 121 | return $this->belongsTo('App\User', 'foreign_key'); | |
| 122 | } | |
| 123 | ||
| 124 | 如果父级模型没有使用 `id` 作为主键,或者是希望用不同的字段来连接子级模型,则可以通过给 `belongsTo` 方法传递第三个参数的形式指定父级数据表的自定义键: | |
| 113 | 125 | |
| 114 | 126 | /** |
| 115 | 127 | * 获得拥有此电话的用户 |
| 116 | 128 | */ |
| 117 | 129 | public function user() |
| 118 | 130 | { |
| 119 | ||
| 120 | ||
| 121 | ||
| 122 | ||
| 123 | ||
| 124 | ||
| 125 | ||
| 126 | ||
| 127 | ||
| 128 | ||
| 129 | 131 | return $this->belongsTo('App\User', 'foreign_key', 'other_key'); |
| 130 | 132 | } |
| 131 | 133 | … | … |
| 151 | 153 | } |
| 152 | 154 | } |
| 153 | 155 | |
| 154 | 记住一点,Eloquent 将会自动确定 | |
| 156 | 记住一点,Eloquent 将会自动确定 `Comment` 模型的外键属性。按照约定,Eloquent 将会使用所属模型名称的 『snake case』形式,再加上 `_id` 后缀作为外键字段。因此,在上面这个例子中,Eloquent 将假定 `Comment` 对应到 `Post` 模型上的外键就是 `post_id`。 | |
| 155 | 157 | |
| 156 | 158 | 一旦关系被定义好以后,就可以通过访问 `Post` 模型的 `comments` 属性来获取评论的集合。记住,由于 Eloquent 提供了『动态属性』 ,所以我们可以像访问模型的属性一样访问关联方法: |
| 157 | 159 | … | … |
| 165 | 167 | |
| 166 | 168 | $comment = App\Post::find(1)->comments()->where('title', 'foo')->first(); |
| 167 | 169 | |
| 168 | 正如 `hasOne` 方法一样,你也可以在使用 | |
| 170 | 正如 `hasOne` 方法一样,你也可以在使用 `hasMany` 方法的时候,通过传递额外参数来覆盖默认使用的外键与本地键: | |
| 169 | 171 | |
| 170 | 172 | return $this->hasMany('App\Comment', 'foreign_key'); |
| 171 | 173 | |
| 172 | 174 | return $this->hasMany('App\Comment', 'foreign_key', 'local_key'); |
| 173 | 175 | |
| 174 | 176 | <a name="one-to-many-inverse"></a> |
| 175 | ### 一对多 | |
| 177 | ### 一对多 (反向) | |
| 176 | 178 | |
| 177 | 179 | 现在,我们已经能获得一篇文章的所有评论,接着再定义一个通过评论获得所属文章的关联关系。这个关联是 `hasMany` 关联的反向关联,需要在子级模型中使用 `belongsTo` 方法定义它: |
| 178 | 180 | … | … |
| 193 | 195 | } |
| 194 | 196 | } |
| 195 | 197 | |
| 196 | ||
| 198 | 这个关系定义好以后,我们就可以通过访问 `Comment` 模型的 `post` 这个『动态属性』来获取关联的 `Post` 模型了: | |
| 197 | 199 | |
| 198 | 200 | $comment = App\Comment::find(1); |
| 199 | 201 | |
| 200 | 202 | echo $comment->post->title; |
| 201 | 203 | |
| 202 | 在上面的例子中,Eloquent 会尝试用 `Comment` 模型的 `post_id` 与 `Post` 模型的 | |
| 204 | 在上面的例子中,Eloquent 会尝试用 `Comment` 模型的 `post_id` 与 `Post` 模型的 `id` 进行匹配。默认外键名是 Eloquent 依据关联名,并在关联名后加上 `_ `再加上主键字段名作为后缀确定的。当然,如果 `Comment` 模型的外键不是 `post_id`,那么可以将自定义键名作为第二个参数传递给 `belongsTo` 方法: | |
| 203 | 205 | |
| 204 | 206 | /** |
| 205 | 207 | * 获取此评论所属文章 | … | … |
| 209 | 211 | return $this->belongsTo('App\Post', 'foreign_key'); |
| 210 | 212 | } |
| 211 | 213 | |
| 212 | 如果父级模型没有使用 `id` 作为主键,或者 | |
| 214 | 如果父级模型没有使用 `id` 作为主键,或者是希望用不同的字段来连接子级模型,则可以通过给 `belongsTo` 方法传递第三个参数的形式指定父级数据表的自定义键: | |
| 213 | 215 | |
| 214 | 216 | /** |
| 215 | 217 | * 获取此评论所属文章 | … | … |
| 218 | 220 | { |
| 219 | 221 | return $this->belongsTo('App\Post', 'foreign_key', 'other_key'); |
| 220 | 222 | } |
| 221 | ||
| 222 | ||
| 223 | 223 | |
| 224 | 224 | <a name="many-to-many"></a> |
| 225 | 225 | ### 多对多 | … | … |
| 406 | 406 | */ |
| 407 | 407 | public $incrementing = true; |
| 408 | 408 | |
| 409 | ||
| 409 | ||
| 410 | 410 | <a name="has-one-through"></a> |
| 411 | 411 | ### 远程一对一关系 |
| 412 | 412 | … | … |
| 907 | 907 | $query->where('content', 'like', 'foo%'); |
| 908 | 908 | }, '>=', 10)->get(); |
| 909 | 909 | |
| 910 | ||
| 910 | ||
| 911 | 911 | <a name="querying-relationship-absence"></a> |
| 912 | 912 | ### 查询不存在的关联 |
| 913 | 913 | … | … |
| 1203 | 1203 | ]; |
| 1204 | 1204 | } |
| 1205 | 1205 | |
| 1206 | ||
| 1206 | ||
| 1207 | 1207 | |
| 1208 | 1208 | #### 嵌套延迟预加载 & `morphTo` |
| 1209 | 1209 |
关于 LearnKu