我是laravel框架的新手。我只是想知道如何从我的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 = \App\Match::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 = \App\Match::mainMatches()
->dotaMatches($request-type)
->where('leagues.type','dota2') // instead of ('league_id','1')
使代码更干净。但是当我链接两个范围时,它说违反SQL约束,因为matchs表和Leagues表都具有状态和类型。有人可以帮助我吗?
答案 0 :(得分:0)
我认为您正在进行public getUser(): Observable<User> {
const myHeader = new HttpHeaders({
'Authorization': 'Basic ' + this.session.getAccessToken(),
}).append('Content-Type', 'application/json');
return this.http
.get<User>(API_URL + '/users', {headers: myHeader})
.pipe(
catchError(this.handleError('get-user', []))
);
}
api.getUser().subscribe(user => this.user = user);
查询:
whereHas
然后,您无需加入// Retrieve all matches with at least one league with the type 'dota2'
$matches = App\Match::whereHas('leagues', function ($query) {
$query->where('type', 'dota2');
})->get();
。