合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 范围查询 ## 定义范围查询 范围查询可以让您轻松的重复利用模型的查询逻辑。要设定范围查询,只要定义有 `scope` 前缀的模型方法: ``` class User extends Model { public function scopePopular($query) { return $query->where('votes', '>', 100); } public function scopeWomen($query) { return $query->whereGender('W'); } } ``` ## 使用范围查询 ``` $users = User::popular()->women()->orderBy('created_at')->get(); ``` ## 动态范围查询 有时您可能想要定义可接受参数的范围查询方法。只要把参数加到方法里: ``` class User extends Model { public function scopeOfType($query, $type) { return $query->whereType($type); } } ``` 然后把参数值传到范围查询方法调用里: ``` $users = User::ofType('member')->get(); ```