[英]Laravel Eloquent Eager loading confusion
在Laravel中,我有一个看起来像这样的模型:
class Recipient extends Model
{
public $table = 'recipients';
public function location()
{
return $this->belongsTo('App\Location');
}
public function teams()
{
return $this->belongsToMany('App\Team');
}
public function company()
{
return $this->belongsTo('App\Company');
}
}
要查询该模型,我这样做:
$recipients = Recipient::with('location')
->with('teams')
->where('company_id',Auth::user()->company_id)
->where('teams.id', 10)
->get();
这样做时,我收到一条错误消息,说laravel无法找到team.id,因为它仅查询父收件人表。 想知道我在做什么错,我认为with
方法是渴望加载/内部联接记录? 我是否需要使用DB:内部联接? 还是我错过了什么?
为此使用whereHas
方法:
Recipient::with('location')
->where('company_id', auth()->user()->company_id)
->whereHas('teams', function($q){
return $q->where('id', 10);
})
->get();
尝试明确,并添加选择语句。 有时,如果不选择,则不会显示关系。 包含ID,否则将无法使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.