【发布时间】:2021-06-04 07:31:31
【问题描述】:
我有一个表以多对多的方式链接到多个其他表。
在我的示例中,pmds(主要医生)是users 表中的记录
- 在
roles表中有一个与pmd对应的role_id;和 - 通过中间表
site_user与sites表关联;或 - 通过中间表
agency_user与agency表关联
User.php下面的函数获取我想要的信息...
public function allPmds()
{
return User::whereHas('role', function ($q) {
$q->where('name', 'pmd');
})->where(function ($q) {
$q->whereHas('siteUsers', function ($q) {
$q->where('site_id', $this->id);
})->orWhereHas('agencies', function ($q) {
$q->where('agency_id', $this->agency_id);
});
});
}
但我真正想要的是渴望加载。像这样的:
public function pmds()
{
return $this->belongsToMany('App\User')->where( <essentially the same code as above> );
}
【问题讨论】:
-
为什么你需要没有数据透视表的多对多关系?请再解释一下。
-
我猜你需要很多通过关系,一切都会很棒。
标签: laravel eloquent pivot relational-database