Skip to content

Commit fc61af4

Browse files
committed
add relationship
1 parent 07690d6 commit fc61af4

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed

index.html

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,109 @@ <h6>Soft Delete <a href="http://laravel.com/docs/5.0/eloquent#soft-deleting" tit
715715
Model::onlyTrashed()->where('cars', 2)->get();
716716
</pre>
717717

718+
<h6>Relationships</h6>
719+
<pre class="prettyprint lang-php">
720+
// One To One - User::phone()
721+
return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
722+
// One To One - Phone::user(), The Inverse Of The Relation
723+
return $this->belongsTo('App\User', 'foreign_key', 'other_key');
724+
725+
// One To Many - Post::comments()
726+
return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
727+
// One To Many - Comment::post()
728+
return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
729+
730+
// Many To Many - User::roles();
731+
return $this->belongsToMany('App\Role', 'user_roles', 'user_id', 'role_id');
732+
// Many To Many - Role::users();
733+
return $this->belongsToMany('App\User');
734+
// Many To Many - Retrieving Intermediate Table Columns
735+
$role->pivot->created_at;
736+
// Many To Many - Pivot table with extra attributes
737+
return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');
738+
// Many To Many - Automatically maintained created_at and updated_at timestamps
739+
return $this->belongsToMany('App\Role')->withTimestamps();
740+
741+
// Has Many Through - Country::posts(), A Country model have
742+
// many Post models through an intermediate User model (User::country_id)
743+
return $this->hasManyThrough('App\Post', 'App\User', 'country_id', 'user_id');
744+
745+
// Polymorphic Relations - Photo::imageable()
746+
return $this->morphTo();
747+
// Polymorphic Relations - Staff::photos()
748+
return $this->morphMany('App\Photo', 'imageable');
749+
// Polymorphic Relations - Product::photos()
750+
return $this->morphMany('App\Photo', 'imageable');
751+
// Polymorphic Relations - Register the morphMap in your AppServiceProvider
752+
Relation::morphMap([
753+
'Post' => App\Post::class,
754+
'Comment' => App\Comment::class,
755+
]);
756+
757+
// Many To Many Polymorphic Relations - Tables: posts,videos,tags,taggables
758+
// Post::tags()
759+
return $this->morphToMany('App\Tag', 'taggable');
760+
// Video::tags()
761+
return $this->morphToMany('App\Tag', 'taggable');
762+
// Tag::posts()
763+
return $this->morphedByMany('App\Post', 'taggable');
764+
// Tag::videos()
765+
return $this->morphedByMany('App\Video', 'taggable');
766+
767+
// Querying Relations
768+
$user->posts()->where('active', 1)->get();
769+
// Retrieve all posts that have at least one comment...
770+
$posts = App\Post::has('comments')->get();
771+
// Retrieve all posts that have three or more comments...
772+
$posts = Post::has('comments', '>=', 3)->get();
773+
// Retrieve all posts that have at least one comment with votes...
774+
$posts = Post::has('comments.votes')->get();
775+
// Retrieve all posts with at least one comment containing words like foo%
776+
$posts = Post::whereHas('comments', function ($query) {
777+
$query->where('content', 'like', 'foo%');
778+
})->get();
779+
780+
// Eager Loading
781+
$books = App\Book::with('author')->get();
782+
$books = App\Book::with('author', 'publisher')->get();
783+
$books = App\Book::with('author.contacts')->get();
784+
785+
// Lazy Eager Loading
786+
$books->load('author', 'publisher');
787+
788+
// Inserting Related Models
789+
$comment = new App\Comment(['message' => 'A new comment.']);
790+
$post->comments()->save($comment);
791+
// save multiple related models
792+
$post->comments()->saveMany([
793+
new App\Comment(['message' => 'A new comment.']),
794+
new App\Comment(['message' => 'Another comment.']),
795+
]);
796+
$post->comments()->create(['message' => 'A new comment.']);
797+
798+
// Updating a belongsTo relationship
799+
$user->account()->associate($account);
800+
$user->save();
801+
$user->account()->dissociate();
802+
$user->save();
803+
804+
// Inserting Related Models - Many To Many Relationships
805+
$user->roles()->attach($roleId);
806+
$user->roles()->attach($roleId, ['expires' => $expires]);
807+
// Detach a single role from the user...
808+
$user->roles()->detach($roleId);
809+
// Detach all roles from the user...
810+
$user->roles()->detach();
811+
$user->roles()->detach([1, 2, 3]);
812+
$user->roles()->attach([1 => ['expires' => $expires], 2, 3]);
813+
814+
// Any IDs that are not in the given array will be removed from the intermediate table.
815+
$user->roles()->sync([1, 2, 3]);
816+
// You may also pass additional intermediate table values with the IDs:
817+
$user->roles()->sync([1 => ['expires' => true], 2, 3]);
818+
819+
</pre>
820+
718821
<h6>Events</h6>
719822
<pre class="prettyprint lang-php">Model::creating(function($model){});
720823
Model::created(function($model){});

index_zh-CN.html

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,107 @@ <h6>软删除 <a href="http://laravel-china.org/docs/5.1/5.0/eloquent#soft-delet
715715
Model::onlyTrashed()->where('cars', 2)->get();
716716
</pre>
717717

718+
<h6>模型关联</h6>
719+
<pre class="prettyprint lang-php">
720+
// 一对一 - User::phone()
721+
return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
722+
// 一对一 - Phone::user(), 定义相对的关联
723+
return $this->belongsTo('App\User', 'foreign_key', 'other_key');
724+
725+
// 一对多 - Post::comments()
726+
return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
727+
// 一对多 - Comment::post()
728+
return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
729+
730+
// 多对多 - User::roles();
731+
return $this->belongsToMany('App\Role', 'user_roles', 'user_id', 'role_id');
732+
// 多对多 - Role::users();
733+
return $this->belongsToMany('App\User');
734+
// 多对多 - Retrieving Intermediate Table Columns
735+
$role->pivot->created_at;
736+
// 多对多 - 中介表字段
737+
return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');
738+
// 多对多 - 自动维护 created_at 和 updated_at 时间戳
739+
return $this->belongsToMany('App\Role')->withTimestamps();
740+
741+
// 远层一对多 - Country::posts(), 一个 Country 模型可能通过中介的 Users
742+
// 模型关联到多个 Posts 模型(User::country_id)
743+
return $this->hasManyThrough('App\Post', 'App\User', 'country_id', 'user_id');
744+
745+
// 多态关联 - Photo::imageable()
746+
return $this->morphTo();
747+
// 多态关联 - Staff::photos()
748+
return $this->morphMany('App\Photo', 'imageable');
749+
// 多态关联 - Product::photos()
750+
return $this->morphMany('App\Photo', 'imageable');
751+
// 多态关联 - 在 AppServiceProvider 中注册你的「多态对照表」
752+
Relation::morphMap([
753+
'Post' => App\Post::class,
754+
'Comment' => App\Comment::class,
755+
]);
756+
757+
// 多态多对多关联 - 涉及数据库表: posts,videos,tags,taggables
758+
// Post::tags()
759+
return $this->morphToMany('App\Tag', 'taggable');
760+
// Video::tags()
761+
return $this->morphToMany('App\Tag', 'taggable');
762+
// Tag::posts()
763+
return $this->morphedByMany('App\Post', 'taggable');
764+
// Tag::videos()
765+
return $this->morphedByMany('App\Video', 'taggable');
766+
767+
// 查找关联
768+
$user->posts()->where('active', 1)->get();
769+
// 获取所有至少有一篇评论的文章...
770+
$posts = App\Post::has('comments')->get();
771+
// 获取所有至少有三篇评论的文章...
772+
$posts = Post::has('comments', '>=', 3)->get();
773+
// 获取所有至少有一篇评论被评分的文章...
774+
$posts = Post::has('comments.votes')->get();
775+
// 获取所有至少有一篇评论相似于 foo% 的文章
776+
$posts = Post::whereHas('comments', function ($query) {
777+
$query->where('content', 'like', 'foo%');
778+
})->get();
779+
780+
// 预加载
781+
$books = App\Book::with('author')->get();
782+
$books = App\Book::with('author', 'publisher')->get();
783+
$books = App\Book::with('author.contacts')->get();
784+
785+
// 延迟预加载
786+
$books->load('author', 'publisher');
787+
788+
// 写入关联模型
789+
$comment = new App\Comment(['message' => 'A new comment.']);
790+
$post->comments()->save($comment);
791+
// Save 与多对多关联
792+
$post->comments()->saveMany([
793+
new App\Comment(['message' => 'A new comment.']),
794+
new App\Comment(['message' => 'Another comment.']),
795+
]);
796+
$post->comments()->create(['message' => 'A new comment.']);
797+
798+
// 更新「从属」关联
799+
$user->account()->associate($account);
800+
$user->save();
801+
$user->account()->dissociate();
802+
$user->save();
803+
804+
// 附加多对多关系
805+
$user->roles()->attach($roleId);
806+
$user->roles()->attach($roleId, ['expires' => $expires]);
807+
// 从用户上移除单一身份...
808+
$user->roles()->detach($roleId);
809+
// 从用户上移除所有身份...
810+
$user->roles()->detach();
811+
$user->roles()->detach([1, 2, 3]);
812+
$user->roles()->attach([1 => ['expires' => $expires], 2, 3]);
813+
814+
// 任何不在给定数组中的 IDs 将会从中介表中被删除。
815+
$user->roles()->sync([1, 2, 3]);
816+
// 你也可以传递中介表上该 IDs 额外的值:
817+
$user->roles()->sync([1 => ['expires' => true], 2, 3]);
818+
718819
<h6>事件</h6>
719820
<pre class="prettyprint lang-php">
720821
Model::creating(function($model){});

0 commit comments

Comments
 (0)