早上好
我目前有一个与 3 个表相关的表。
我想到要执行“belongsToMany”,但我不知道如何处理 3 个关系
表格:
schools_series(关系)
- ID
- cycle_id
- 学校 ID
- serie_id
循环
- ID
- 活跃(真/假)
- ** 更多列 **
学校
- ID
- ** 更多列 **
系列
- ID
- ** 更多列 **
主要模型:
学校
class School 扩展了 Model 实现 Presentable { 使用 PresentableTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'network_id',
'fantasy_name',
'social_name',
'email',
'sim_web_code',
'state_registration',
'cnpj',
'status',
'start_date',
];
public function series()
{
return $this->belongsToMany(Serie::class, 'schools_series')->where('id_cycle', 'xxxx');
}
我需要能够根据周期中具有“active”的“cycle_id”取出“学校系列”
示例 SQL:
SELECT ss.* FROM schools as s
INNER JOIN schools_series as ss
ON ss.school_id = s.id
INNER JOIN cycle as c
ON ss.cycle_id = c.id AND c.active = 1
WHERE s.id = 115
最佳答案
解决方案:
public function series()
{
return $this->belongsToMany(Serie::class, 'schools_series')->withPivot('cycle_id')->join('cycle', 'cycle.id', '=', 'schools_series.cycle_id')->where('cycle.active', '=', 1);
}
关于laravel - 属于ToMany多个外键,laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51396677/