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 关注
- 138 浏览
添加回答
举报