<?php
/***
* 关联关系也是强大的查询构建器
* 定义关联关系为方法可以提供功能强大的方法链和查询能力
*/
$user->posts()->where('active', 1)->get();//我们可以添加更多约束条件到 posts 关联关系
class User extends Model
{
/**
* 获取关联到用户的手机
*/
public function phone()
{
//一个 User 模型有一个与之关联的 Phone 模型
return $this->hasOne('App\Phone');//一对一
//Eloquent 默认关联关系的外键基于模型名称,在本例中,
//Phone 模型默认有一个 user_id 外键,
//如果你希望覆盖这种约定,可以传递第二个参数到 hasOne 方法
return $this->hasOne('App\Phone', 'foreign_key');
//如果你想要关联关系使用其他值而不是 id,可以传递第三个参数到hasOne 来指定自定义的主键
//return $this->hasOne('App\Phone', 'user_id', 'id');例子
return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
}
}
/***
* 我们可以从 User 中访问 Phone 模型,相应地,也可以在 Phone 模型中定义关联关系从而让我们可以拥有该手机的 User
* 我们可以使用 belongsTo 方法定义与 hasOne 关联关系相对的关联
*/
class Phone extends Model{
/**
* 获取拥有该手机的用户
*/
public function user()
{
return $this->belongsTo('App\User');//定义相对的关联
return $this->belongsTo('App\User', 'foreign_key');
return $this->belongsTo('App\User', 'foreign_key', 'other_key');
//return $this->belongsTo('App\User', 'user_id', 'id');例子
}
}
class Post extends Model{
/**
* 获取博客文章的评论
*/
public function comments()
{
return $this->hasMany('App\Comment');//一对多
return $this->hasMany('App\Comment', 'foreign_key');
return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
// 在本例中,传递完整参数代码如下
return $this->hasMany('App\Comment', 'post_id', 'id');
}
}
class Comment extends Model{
/**
* 获取评论对应的博客文章
*/
public function post(){
return $this->belongsTo('App\Post', 'foreign_key');
return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
return $this->belongsTo('App\Post', 'post_id', 'id');
}
}
$comments = App\Post::find(1)->comments;//一对多使用举例
foreach ($comments as $comment) {
//
}
$comments = App\Post::find(1)->comments()->where('title', 'foo')->first();
//反向
$comment = App\Comment::find(1);
echo $comment->post->title;
class User extends Model{
/**
* 用户角色
*/
public function roles()
{
return $this->belongsToMany('App\Role');//多对多
return $this->belongsToMany('App\Role', 'user_roles');
return $this->belongsToMany('App\Role', 'user_roles', 'user_id', 'role_id');
}
}
$user = App\User::find(1);
foreach ($user->roles as $role) {//关联关系被定义之后,可以使用动态属性 roles 来访问用户的角色
//
}
//可以调用 roles 方法来添加条件约束到关联查询上
$roles = App\User::find(1)->roles()->orderBy('name')->get();
//定义相对的关联关系
class Role extends Model{
/**
* 角色用户
*/
public function users()
{
return $this->belongsToMany('App\User');
//默认情况下,只有模型主键才能用在 pivot 对象上,如果你的 pivot 表包含额外的属性,必须在定义关联关系时进行指定
return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');
//如果你想要你的 pivot 表自动包含created_at 和 updated_at 时间戳,在关联关系定义时使用 withTimestamps 方法
return $this->belongsToMany('App\Role')->withTimestamps();
//将中间表访问器改为 subscription 来取代 pivot,这可以通过在定义关联关系时使用 as 方法来实现
return $this->belongsToMany('App\Podcast')
->as('subscription')
->withTimestamps();
//在定义关联关系的时候使用 wherePivot 和 wherePivotIn 方法过滤 belongsToMany 返回的结果集
return $this->belongsToMany('App\Role')->wherePivot('approved', 1);
return $this->belongsToMany('App\Role')->wherePivotIn('priority', [1, 2]);
}
}
/***
* 处理多对多关联要求一个中间表。Eloquent 提供了一些有用的方法来与这个中间表进行交互,
* 例如,我们假设 User 对象有很多与之关联的 Role 对象,访问这些关联关系之后,我们可以使用这些模型上的 pivot 属性访问中间表
*/
$user = App\User::find(1);
foreach ($user->roles as $role) {
echo $role->pivot->created_at;
}
//中间表访问器改为 subscription 来取代 pivot,定义好之后,就可以使用自定义的属性名来访问中间表数据了
$users = User::with('podcasts')->get();
foreach ($users->flatMap->podcasts as $podcast) {
echo $podcast->subscription->created_at;
}
class Role extends Model
{
/**
* The users that belong to the role.
*/
public function users()
{
return $this->belongsToMany('App\User')->using('App\UserRole');//自定义中间表模型
}
}
use Illuminate\Database\Eloquent\Relations\Pivot;
class UserRole extends Pivot
{
//
}
laravel核心概念学习之Eloquent(关联关系)
最新推荐文章于 2024-05-04 16:25:44 发布