您的位置:首页 > 编程语言 > PHP开发

laravel的Eloquent关联关系

2017-01-08 13:48 148 查看
1.简介:
1>Eloquent 关联关系以Eloquent模型类方法的形式被定义(是模型类的一个方法)。
2>同 Eloquent 模型本身一样,关联关系也是强大的查询构建器,定义关联关系为函数能够提供功能强大的方法链和查询能力(也支持一般的where、get、find等方法。
$user->posts()->where('active', 1)->get();

2.定义关联关系:
1>一对一
目标:一个User模型有一个与之对应的Phone模型(以User模型为主)
实现步骤:
1.在User模型中,创建phone()方法
2.在phone()方法中,返回 Eloquent 模型基类上的 'hasOne()' 方法的执行结果
代码:
class User extends Model{
public function phone(){
return $this->hasOne('App\Phone');		// 参数为 'User模型关联的Phone模型'
}
}
hasOne()方法的3种不同的使用情况:
Eloquent 默认关联关系的外键基于模型名称。默认是:Phone模型的主键是id,关联的外键是user_id
1.hasOne('App\Phone') - 默认情况
2.hasOne('App\Phone', 'foreign_key') - Phone模型的主键是id,但是外键不是user_id
3.hasOne('App\Phone', 'foreign_key', 'local_key') - Phone模型的主键不是id,外键也不是user_id
调用:
关联关系被定义后,可以使用 Eloquent 的 '动态属性' 来获取关联关系!
注意:
动态属性:允许我们访问关联函数,就像它们是定义在模型上的属性一样!
$phone = User::find(1)->phone;		// 理解 '动态属性' 的概念:按理说,我们定义了 phone() 方法,应该调用的是一个方法,而这里将其作为了一个 '属性' 来调用!

-----------
定义相对的关联
-----------

目标:我们可以从User中访问Phone模型,相应的,我们也可以在Phone模型中定义关联关系从而让我们可以拥有该phone的User。
实现步骤:
1.在Phone模型中,创建user()方法
2.在user()方法中,返回 Eloquent 模型基类上的 'belongsTo()' 方法的执行结果
代码:
class Phone extends Model{
public function user(){
return $this->belongsTo('App\User');		// 参数为 'Phone模型关联的User模型'
}
}
belongsTo()方法的3种不同的使用情况:
Eloquent 默认关联关系的外键基于模型名称。默认是:Phone模型的主键是id,关联的外键是user_id
1.belongsTo('App\User') - 默认情况
2.belongsTo('App\User', 'foreign_key') - Phone模型的主键是id,但是外键不是user_id
3.belongsTo('App\User', 'foreign_key', 'local_key') - Phone模型的主键不是id,外键也不是user_id
调用:
$user = Phone::find(1)->user;

总结:
不管是User模型类,还是Phone模型类,2者都是以 'User' 模型为主。Phone模型还是附属于User模型。Phone模型具有外键 'user_id'

2>一对多
定义和使用基本同 '一对一' 一样,只是对应的方法改变了,以 '一个帖子,有多个评论' 为例:
hasMany() - 一个帖子有多个评论
belongsTo() - 一个评论,必然只针对一个帖子(所以,反过来,它还是 'belongsTo' 方法,并未改变)
3>多对多
稍微复杂一点。这种关联关系的一个典型例子是:一个用户有多个角色,同时一个角色被多个用户共用。要定义这样的关联关系,需要三个数据表:
users、roles和role_user,role_user表按照关联模型名的字母顺序命名,并且包含user_id和role_id两个列。
实现步骤:
1.在User模型中,创建roles()方法
2.在roles()方法中,返回 Eloquent 模型基类上的 'belongsToMany()' 方法的执行结果
代码:
class User extends Model{
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
同样belongsToMany()使用3种方式调用。

-----------
定义相对的关联
-----------

同上面一样,只不过是翻过来了。一样使用的是 'belongsToMany()' 方法

-------------------------
获取中间表的列(本例是:role_user)
-------------------------
假设User对象有很多与之关联的Role对象,访问这些关联关系之后,我们可以使用模型上的pivot属性访问中间表:
$user = App\User::find(1);
foreach ($user->roles as $role) {
echo $role->pivot->created_at;		// 每一个 '$role' 角色对象,都有一个 'pivot' 属性
}
pivot属性,包含一个代表中间表的模型,并且可以像其它 Eloquent 模型一样使用。
如果pivot表包含额外的属性(中间表,包含额外的字段),必须在定义关联关系时指定:
return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');
pivot表自动包含created_at和updated_at时间戳,在关联关系定义时使用withTimestamps方法:
return $this->belongsToMany('App\Role')->withTimestamps();

4>远层的一对多:
countries
id - integer
name - string

users
id - integer
country_id - integer
name - string

posts
id - integer
user_id - integer
title - string

目标:
查看一个国家有哪些帖子

5>多态关联:
posts
id - integer
title - string
body - text

comments
id - integer
post_id - integer
body - text

likes
id - integer
likeable_id - integer
likeable_type - string 		// post | comment,喜欢的是贴子还是评论
目标:
查询贴子的喜欢数
评论的喜欢数
喜欢的这条记录,是贴子还是评论

6>多对多的多态关联:
posts
id - integer
name - string

videos
id - integer
name - string

tags
id - integer
name - string

taggables ------------ 是不是应该还有个 'id' 主键自增字段
tag_id - integer
taggable_id - integer
taggable_type - string
目标:
贴子的标签
视频的标签
标签下的所有帖子
标签下的所有视频
打标签的这条记录,是'什么标签',类型是帖子还是视频

3.关联查询:

4.插入关联模型:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: