1 回答

TA貢獻1848條經驗 獲得超2個贊
如果您需要避免特定查詢中的全局范圍,則 yuu 應使用withoutGlobalScope:
Students::withoutGlobalScope('onlyGraduated')->where("graduated",1)->get();
我將在您的模型中創建一個名為graduated和 的本地范圍leaver:
public function scopeGraduated($query) {
return $query->withoutGlobalScope('onlyGraduated')->where("graduated",1);
}
public function scopeLeavers($query) {
return $query->withoutGlobalScope('onlyLeaver')->where("leaver",1);
}
然后您將能夠使用以下方式查詢數據:
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 關注
- 167 瀏覽
添加回答
舉報