Web后端初探(9)--THINKPHP的SQL查询范围

应用场景:某一片段的数据需要反复查询,于是我们可以将代码封装在一个方法里.

在model中写道:

protected function scope+驼峰命名($query)
{
 $query->where('data','这个是5号');
} 

使用的时候直接调用

$result=User::scope('data,id')->all();  //调用方法同时筛选查询data,id



综合应用:

$list=Data::scope('id') //先查询id的筛选
 ->scope('data') //查询data的筛选
 ->scope(function($query){
   $query->order('userid','desc');
   })  //id降序排列
 ->all();
print_r($list);

猜你喜欢

转载自blog.csdn.net/qq_39987002/article/details/80876423