在Laravel我有一个看起来像这样的模型:
select country_id,country_name from rptavlview.country where solr_query='{"q":"*:*","paging": null,"start":1}' limit 5;
要查询该模型,我执行此操作:
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');
}
}
执行此操作时,我收到一条错误消息,指出laravel无法找到teams.id,因为它只查询父收件人表。想知道我做错了什么,我认为$recipients = Recipient::with('location')
->with('teams')
->where('company_id',Auth::user()->company_id)
->where('teams.id', 10)
->get();
方法是急切加载/内连接记录?我需要使用DB:内部连接吗?或者我错过了什么?
答案 0 :(得分:1)
使用whereHas
方法:
Recipient::with('location')
->where('company_id', auth()->user()->company_id)
->whereHas('teams', function($q){
return $q->where('id', 10);
})
->get();
答案 1 :(得分:0)
尝试显式并添加一个select语句。有时未选择时,关系不会显示。包括其他无效的ID