Thinkphp5学习(16)查询范围
学习内容:
视频教程:
https://www.kancloud.cn/tpshop/thinkphp5/228855
完全开发手册:https://www.kancloud.cn/manual/thinkphp5/135196
教程中的代码:https://github.com/phpervip/tp5a
TP5查询范围
scope+查询范围名称
// email查询
model里写:
protected function scopeEmail($query){
$query->where("email","tpshop@tpshop.cn");
}
protected function scopeEmail($query,$a){
$query->where($email,$a);
}
// level查询
protected function scopeLevel($query){
$query->where("level",1);
}
// 在控制器里
$list = Users::scope("email,level")->all();
print_r($list);
$list = Users::scope("email","vip@adads.com")->all();
print_r($list);
$list = Users::scope("email")->scope("level")->scope(function($query){
$query->order("user_id","desc");
}
)->all();
print_r($list);
// 全局查询范围
protected static function base($query){
$query->where("user_id",1);
}
// 这样,控制器里不写,也会加上这一条件。
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: ThinkPHP5学习(8)数据库-基本操作
- 下一篇: Thinkphp5学习(24)模型输出
