方法名写错
winter-ice
5年前
修改理由:
相关信息:
- 类型:文档文章
- 文章: 模型关联
- 文档: 《Laravel 5.7 中文文档(5.7)》
此投稿已在 5年前 合并。
内容修改:
Old | New | Differences |
---|---|---|
1 | ||
2 | 1 | # Eloquent: 关联 |
3 | 2 | |
4 | 3 | - [简介](#introduction) | … | … |
121 | 120 | return $this->belongsTo('App\User', 'foreign_key', 'other_key'); |
122 | 121 | } |
123 | 122 | |
124 | ||
123 | ||
125 | 124 | <a name="one-to-many"></a> |
126 | 125 | ### 一对多 |
127 | 126 | … | … |
212 | 211 | return $this->belongsTo('App\Post', 'foreign_key', 'other_key'); |
213 | 212 | } |
214 | 213 | |
215 | ||
214 | ||
216 | 215 | <a name="many-to-many"></a> |
217 | 216 | ### 多对多 |
218 | 217 | … | … |
318 | 317 | echo $podcast->subscription->created_at; |
319 | 318 | } |
320 | 319 | |
321 | ||
320 | ||
322 | 321 | |
323 | 322 | #### 通过中间表列过滤关系 |
324 | 323 | … | … |
522 | 521 | |
523 | 522 | 你可以在 `AppServiceProvider` 中的 `boot` 函数中使用 `Relation::morphMap` 方法注册「多态映射表」,或者使用一个独立的服务提供者注册。 |
524 | 523 | |
525 | ||
524 | ||
526 | 525 | <a name="many-to-many-polymorphic-relations"></a> |
527 | 526 | ### 多对多的多态关联 |
528 | 527 | … | … |
768 | 767 | echo $posts[0]->comments_count; |
769 | 768 | |
770 | 769 | echo $posts[0]->pending_comments_count; |
771 | ``` | |
770 | ``` | |
772 | 771 | <a name="eager-loading"></a> |
773 | 772 | ## 懒加载 |
774 | 773 | … | … |
889 | 888 | }]); |
890 | 889 | ``` |
891 | 890 | |
892 | 当关联关系没有被加载时,你可以使用使用 `loadMissi | |
891 | 当关联关系没有被加载时,你可以使用使用 `loadMissing` 方法: | |
893 | 892 | |
894 | 893 | ``` |
895 | 894 | public function format(Book $book) | … | … |
903 | 902 | } |
904 | 903 | ``` |
905 | 904 | |
906 | ||
905 | ||
907 | 906 | |
908 | 907 | <a name="inserting-and-updating-related-models"></a> |
909 | 908 | ## 插入 & 更新关联模型 | … | … |
1008 | 1007 | }); |
1009 | 1008 | } |
1010 | 1009 | |
1011 | ||
1010 | ||
1012 | 1011 | |
1013 | 1012 | <a name="updating-many-to-many-relationships"></a> |
1014 | 1013 | ### 多对多关联 |