【发布时间】:2018-11-25 05:15:20
【问题描述】:
现在我有这些方法:
程序.php
public function institute()
{
return $this->belongsTo(Institute::class, 'institute_id')->withTrashed();
}
用户.php
public function programmes()
{
// These programmes belongs to the same institute
return $this->belongsToMany(Programme::class);
}
public function getInstituteAttribute()
{
return $this->programmes->first()->institute ?? null;
}
如何通过一个 SQL 查询获取机构并保持关系?
因为现在我将程序提取到集合中,然后我从 DB 中提取机构,并且沿途关系松散。
想要类似的东西:
public function institute()
{
return $this->programmes()->first()->institute();
}
【问题讨论】:
-
提供更多信息
-
@MadhuNair 给你
标签: php laravel relationship