1 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊
如果您需要避免特定查詢中的全局范圍,則 yuu 應(yīng)使用withoutGlobalScope:
Students::withoutGlobalScope('onlyGraduated')->where("graduated",1)->get();
我將在您的模型中創(chuàng)建一個(gè)名為graduated和 的本地范圍leaver:
public function scopeGraduated($query) {
return $query->withoutGlobalScope('onlyGraduated')->where("graduated",1);
}
public function scopeLeavers($query) {
return $query->withoutGlobalScope('onlyLeaver')->where("leaver",1);
}
然后您將能夠使用以下方式查詢數(shù)據(jù):
Students::get(); // Only students with both graduated and leaver null
Students::graduated()->get(); // Students with leaver null and graduated 1
Students::leavers()->get(); // Students with leaver 1 and graduated null
- 1 回答
- 0 關(guān)注
- 185 瀏覽
添加回答
舉報(bào)