调用一个模型中的2个联接函数



我想知道如何从我的Match模型中链接两个作用域,这里有两个作用:

public function scopeMainMatches($query){
return $query->where('matches.type','main');
}

public function scopeDotaMatches($query,$type){
return $query
->join('leagues','leagues.id','=','matches.id')
->select('matches.*')
->where('leagues.type',$type);
}

这是我的控制器,它返回表联盟的ID联盟:

public function showDotaMatches($ptr){   
$_matches = AppMatch::mainMatches()
->whereNotIn('status',['ongoing','open'])
->get()
->load('league','teamA', 'teamB')
->where('league_id','1')
->sortByDesc('schedule')
->slice($ptr, 10);

我想要的是:

public function showDotaMatches($ptr,$request){
$_matches = AppMatch::mainMatches()
->dotaMatches($request-type)
->where('leagues.type','dota2') // instead of ('league_id','1')

以使代码干净。但当我把这两个作用域链接起来时,它说SQL约束违反,因为比赛表和联赛表都有状态和类型。有人能帮我吗?

我想您正在寻找whereHas查询:

// Retrieve all matches with at least one league with the type 'dota2'
$matches = AppMatch::whereHas('leagues', function ($query) {
$query->where('type', 'dota2');
})->get();

那么您就不需要加入scopeDotaMatches

最新更新