One To One
Assume that Usermodel is associated with the Phone model. To define such an association, you need to define a phone method in the User model, which returns An association defined by the hasOne method
<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class User extends Model{ /** * Get the phone record associated with the user. */ public function phone() { return $this->hasOne('App\Phone'); } }
The first parameter of the hasOne method is the model to be associated. After it is defined, you can use the following syntax to query the associated attributes
$phone = User::find(1)->phone;
Eloquent will assume the association The foreign key is based on the model name, so the Phone model will automatically use the user_id field as the foreign key, which can be overridden using the second and third parameters
return $this->hasOne('App\Phone', 'foreign_key');return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
After defining the above model, you can use the User model to obtain the Phone model. Of course, you can also obtain the corresponding User through the Phone model. This uses the belongsTo method
<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Phone extends Model{ /** * Get the user that owns the phone. */ public function user() { return $this->belongsTo('App\User'); // return $this->belongsTo('App\User', 'foreign_key'); // return $this->belongsTo('App\User', 'foreign_key', 'other_key'); } }
Suppose there is a post with a lot of associated comment information. In this case, a one-to-many association should be used, using the hasMany method
<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Post extends Model{ /** * Get the comments for the blog post. */ public function comments() { return $this->hasMany('App\Comment'); } }
Query operation
$comments = App\Post::find(1)->comments; foreach ($comments as $comment) { //} $comments = App\Post::find(1)->comments()->where('title', 'foo')->first();
Reverse association also uses the belongsTo method, refer to the One To One section.
$comment = App\Comment::find(1); echo $comment->post->title;
Many-to-many association is more complicated to implement than hasOne and hasMany because there is an additional intermediate table.
Consider a scenario where a user can belong to multiple roles, and a role can also belong to multiple users. This introduces three tables: users, roles, role_user. The role_user table is a related table and contains two fields user_id and role_id.
Many-to-many association requires the belongsToMany method
<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class User extends Model{ /** * The roles that belong to the user. */ public function roles() { // 指定关联表 // return $this->belongsToMany('App\Role', 'role_user'); // 指定关联表,关联字段 // return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id'); return $this->belongsToMany('App\Role'); } }
The above defines that a user belongs to multiple roles. Once the relationship is established, you can query it
user = App\User::find(1); foreach ($user->roles as $role) { //}$roles = App\User::find(1)->roles()->orderBy('name')->get();
The reverse relationship is implemented the same as the forward relationship
<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Role extends Model{ /** * The users that belong to the role. */ public function users() { return $this->belongsToMany('App\User'); } }
For many-to-many relationships, an intermediate table is introduced, so There needs to be a way to query the column values of the intermediate table, such as the time when the relationship was established, etc. Use the pivot attribute to query the intermediate table
$user = App\User::find(1); foreach ($user->roles as $role) { echo $role->pivot->created_at; }
The above code accesses the created_at field of the intermediate table.
Note that by default, the keys of the subsequent models can be accessed through the pivotobject. If the intermediate table contains additional attributes, you need to use the withPivot method when specifying the association. Explicitly specify the column name
return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');
This relationship is relatively powerful. Suppose there is a scenario: the Country model contains multiple User models, and each User model contains Multiple Post models, that is to say, there are many users in a country, and these users have many posts. We want to query all posts in a certain country. How to achieve this? This uses the Has Many Through relationship
countries id - integer name - stringusers id - integer country_id - integer name - stringposts id - integer user_id - integer title - string
As you can see, the posts table does not directly contain country_id, but it establishes a relationship with the countries table through the users table.
Using the Has Many Through relationship
namespace App; use Illuminate\Database\Eloquent\Model;class Country extends Model{ /** * Get all of the posts for the country. */ public function posts() { // return $this->hasManyThrough('App\Post', 'App\User', 'country_id', 'user_id'); return $this->hasManyThrough('App\Post', 'App\User'); } }
The first parameter of the method hasManyThrough is the name of the model we wish to access, and the second parameter is the intermediate model name.
HasManyThrough hasManyThrough( string $related, string $through, string|null $firstKey = null, string|null $secondKey = null, string|null $localKey = null)
Polymorphic relations allow the same model to belong to multiple different models using one association. Assume such a scenario, we have a post table and In a comment table, users can like both posts and comments. How to deal with this situation?
The table structure is as follows
posts id - integer title - string body - textcomments id - integer post_id - integer body - textlikes id - integer likeable_id - integer likeable_type - string
As you can see, we use the likeable_type field in the likes table to determine whether the record likes a post or a comment. With the table structure in place, it is time to define the model.
<?php namespace App; use Illuminate\Database\Eloquent\Model;class Like extends Model{ /** * Get all of the owning likeable models. */ public function likeable() { return $this->morphTo(); } }class Post extends Model{ /** * Get all of the product's likes. */ public function likes() { return $this->morphMany('App\Like', 'likeable'); } }class Comment extends Model{ /** * Get all of the comment's likes. */ public function likes() { return $this->morphMany('App\Like', 'likeable'); } }
By default, the type of likeable_type is the complete name of the associated model, such as App\Post and App\Comment here.
Normally we may use a custom value to identify the associated table name. Therefore, we need to customize this value. We need to register the association in the boot method of the project's service provider object. , for example, in the boot method of AppServiceProvider
use Illuminate\Database\Eloquent\Relations\Relation;Relation::morphMap([ 'posts' => App\Post::class, 'likes' => App\Like::class,]);
Access all likes of a post
$post = App\Post::find(1); foreach ($post->likes as $like) { //}
Access a liked post or comment
$like = App\Like::find(1); $likeable = $like->likeable;
In the above example, the returned likeable will return posts or comments based on the type of the record.
Many-to-many association uses the methods morphToMany and morphedByMany, there will be no more nonsense here
In Eloquent, all relationships are defined using functions , and related instances can be obtained without executing related queries. Suppose we have a blog system, and the User model is associated with many Post models:
/** * Get all of the posts for the user. */public function posts() { return $this->hasMany('App\Post'); }
You can query the association as follows and add additional constraints
$user = App\User::find(1);$user->posts()->where('active', 1)->get();
If not needed Add constraints to the associated attributes, which can be accessed directly as attributes of the model. For example, in the above example, we can use the following method to access User's Post
$user = App\User::find(1);foreach ($user->posts as $post) { //}
动态的属性都是延迟加载的,它们只有在被访问的时候才会去查询数据库,与之对应的是预加载,预加载可以使用关联查询出所有数据,减少执行sql的数量。
使用has方法可以基于关系的存在性返回结果
// 检索至少有一个评论的所有帖子...$posts = App\Post::has('comments')->get(); // Retrieve all posts that have three or more comments...$posts = Post::has('comments', '>=', 3)->get(); // Retrieve all posts that have at least one comment with votes...$posts = Post::has('comments.votes')->get();
如果需要更加强大的功能,可以使用whereHas和orWhereHas方法,把where条件放到has语句中。
// 检索所有至少存在一个匹配foo%的评论的帖子$posts = Post::whereHas('comments', function ($query) { $query->where('content', 'like', 'foo%'); })->get();
在访问Eloquent模型的时候,默认情况下所有的关联关系都是延迟加载的,在使用的时候才会开始加载,这就造成了需要执行大量的sql的问题,使用预加载功能可以使用关联查询出所有结果
<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Book extends Model{ /** * Get the author that wrote the book. */ public function author() { return $this->belongsTo('App\Author'); } }
接下来我们检索所有的书和他们的作者
$books = App\Book::all(); foreach ($books as $book) { echo $book->author->name; }
上面的查询将会执行一个查询查询出所有的书,然后在遍历的时候再执行N个查询查询出作者信息,显然这样做是非常低效的,幸好我们还有预加载功能,可以将这N+1个查询减少到2个查询,在查询的时候,可以使用with方法指定哪个关系需要预加载。
$books = App\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, ...)
预加载多个关系
$books = App\Book::with('author', 'publisher')->get();
嵌套的预加载
$books = App\Book::with('author.contacts')->get();
$users = App\User::with(['posts' => function ($query) { $query->where('title', 'like', '%first%'); }])->get();$users = App\User::with(['posts' => function ($query) { $query->orderBy('created_at', 'desc'); }])->get();
延迟预加载#
有时候,在上级模型已经检索出来之后,可能会需要预加载关联数据,可以使用load方法
$books = App\Book::all();if ($someCondition) { $books->load('author', 'publisher'); }$books->load(['author' => function ($query) { $query->orderBy('published_date', 'asc'); }]);
保存单个关联模型
$comment = new App\Comment(['message' => 'A new comment.']); $post = App\Post::find(1);$post->comments()->save($comment);
$post = App\Post::find(1); $post->comments()->saveMany([ new App\Comment(['message' => 'A new comment.']), new App\Comment(['message' => 'Another comment.']), ]);
多对多关联可以为save的第二个参数指定关联表中的属性
App\User::find(1)->roles()->save($role, ['expires' => $expires]);
上述代码会更新中间表的expires字段。
使用create方法与save方法的不同在于它是使用数组的形式创建关联模型的
$post = App\Post::find(1);$comment = $post->comments()->create([ 'message' => 'A new comment.',]);
更新belongsTo关系的时候,可以使用associate方法,该方法会设置子模型的外键
$account = App\Account::find(10); $user->account()->associate($account); $user->save();
要移除belongsTo关系的话,使用dissociate方法
$user->account()->dissociate();$user->save();
中间表查询条件#
当查询时需要对使用中间表作为查询条件时,可以使用wherePivot
, wherePivotIn
,orWherePivot
,orWherePivotIn
添加查询条件。
$enterprise->with(['favorites' => function($query) { $query->wherePivot('enterprise_id', '=', 12)->select('id'); }]);
Attaching / Detaching#
$user = App\User::find(1); // 为用户添加角色 $user->roles()->attach($roleId); // 为用户添加角色,更新中间表的expires字段 $user->roles()->attach($roleId, ['expires' => $expires]); // 移除用户的单个角色 $user->roles()->detach($roleId); // 移除用户的所有角色 $user->roles()->detach();
attach和detach方法支持数组参数,同时添加和移除多个
$user = App\User::find(1); $user->roles()->detach([1, 2, 3]); $user->roles()->attach([1 => ['expires' => $expires], 2, 3]);
使用updateExistingPivot方法更新中间表
$user = App\User::find(1);$user->roles()->updateExistingPivot($roleId, $attributes);
同步中间表(同步关联关系)#
使用sync方法,可以指定两个模型之间只存在指定的关联关系
$user->roles()->sync([1, 2, 3]); $user->roles()->sync([1 => ['expires' => true], 2, 3]);
上述两个方法都会让用户只存在1,2,3三个角色,如果用户之前存在其他角色,则会被删除。
更新父模型的时间戳#
假设场景如下,我们为一个帖子增加了一个新的评论,我们希望这个时候帖子的更新时间会相应的改变,这种行为在Eloquent中是非常容易实现的。
在子模型中使用$touches属性实现该功能
<?php namespace App; use Illuminate\Database\Eloquent\Model;class Comment extends Model{ /** * All of the relationships to be touched. * * @var array */ protected $touches = ['post']; /** * Get the post that the comment belongs to. */ public function post() { return $this->belongsTo('App\Post'); } }
现在,更新评论的时候,帖子的updated_at字段也会被更新
$comment = App\Comment::find(1);$comment->text = 'Edit to this comment!';$comment->save();
The above is the detailed content of Laravel framework - Detailed introduction to the advanced parts of EloquentORM. For more information, please follow other related articles on the PHP Chinese website!