AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
### 定义范围查询 范围查询可以让您轻松的重复利用模型的查询逻辑。要设定范围查询,只要定义有 `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(); ~~~ ~~~