public function mocha() //模型查询 { // 取出主键为5的数据 $Attr = Attr::get(5); echo $Attr->name; // 使用查询构造器查询满足条件的数据 $Attr = Attr::where('name', 'thinkphp')->find(); echo $Attr->name; // 使用查询构造器查询满足条件的数据 $Attr = Attr::where('name', 'thinkphp')->findOrEmpty(); // 根据主键获取多个数据 $list = Attr::all('15,16,17'); //或者使用数组 $list = Attr::all([1,2,3]); //对数据集进行遍历操作 foreach($list as $key=>$user) { echo $user->id."<br/>"; } // 使用查询构造器查询 $list = Attr::where('status', 1)->limit(3)->order('id', 'asc')->select(); foreach($list as $key=>$user) { echo $user."<br/>"; } // 使用查询构造器查询 $list = Attr::where('status', 1)->limit(3)->order('id', 'asc')->all(); foreach($list as $key=>$user) { echo $user."<br/>"; } $Attr= Attr::where('id',10)->find(); $Attr= Attr::where('status',1)->order('id','desc')->select(); $Attr= Attr::where('status',1)->limit(10)->select(); // 获取某个用户的积分 $Attr= Attr::where('id',8)->value('status'); // 获取某个列的所有值 $Attr= Attr::where('status',1)->column('name'); // 以id为索引 $Attr= Attr::where('status',1)->column('name','id'); //动态查询 // 根据name字段查询用户 $Attr = Attr::getByName('thinkphp'); //根据email字段查询用户 $Attr = Attr::getByColor('thinkphp@qq.com'); //聚合查询 $Attr =Attr::count();//count统计数量,参数是要统计的字段名(可选) $Attr =Attr::where('status','>',0)->count(); $Attr =Attr::where('status',1)->avg('id');//avg获取平均值,参数是要统计的字段名(必须) $Attr =Attr::where('status',1)->sum('id');//sum获取总值,参数是要统计的字段名(必须) $Attr =Attr::max('id'); //max获取最大值 //数据分批处理 $Attr = Attr::chunk(100,function($users) { foreach($users as $user){ // 处理user模型对象 } }); //使用游标查询 foreach(Attr::where('status', 1)->cursor() as $user){ echo $user->name."<br/>"; } //查询缓存 //get方法和all方法的支持使用查询缓存,可以直接在第二个参数传入true表示开启查询缓存。 $Attr = Attr::get(1,true); $Attr = Attr::all('1,2,3',true); //如果要设置缓存标识,则必须在第三个参数传入缓存标识。 $Attr = Attr::get(1,'','user'); $Attr = Attr::all('1,2,3','','user_list'); $Attr = new Attr; $Attr->name = 'thinkphp'; $Attr->color = 'thinkphp@qq.com'; $Attr->create_time=time(); $Attr->save(); // 从主库读取数据 $Attr->master()->get($Attr->id); $Attr = new Attr; $Attr->name = 'thinkphp'; $Attr->color = 'thinkphp@qq.com'; $Attr->create_time=time(); $Attr->save(); // 从主库读取数据 $Attr->readMaster(true)->get($Attr->id); // var_dump($Attr); return json($Attr); } public function model() //模型删除 { // 删除ID为11 $user = Attr::get(11); $data=$user->delete(); //删除主键 为 3 $data =Attr::destroy(3); //支持批量删除多个数据 $data =Attr::destroy('1,2,3'); //或者 $data =Attr::destroy([1,2,3]); $data= Attr::where('id','=',4)->delete(); var_dump($data); // return json($data); } public function mogeng()//模型更新 { $user = Attr::get(11); $user->name = 'thinkphp'; $user->color = 'thinkphp@qq.com'; $data= $user->save(); $user = Attr::where('status',2) ->where('name','liuchen') ->find(); $user->name = 'php'; $user->color = 'thinkphp@qq.com'; $data= $user->save(); $user = Attr::get(11); $user->name = 'thinkphp'; $user->color = 'thinkphp@qq.com'; $user->status = Db::raw('status+1'); $data= $user->save(); $user = Attr::get(11); $user->name = 'tp'; $user->color = 'tp@qq.com'; $user->status = ['inc', 1]; $data=$user->save(); $user = new Attr; //新增 $user->id='11'; $user->name = 'thinkphp'; $user->color = 'thinkphp@qq.com'; $user->create_time= time(); $data = $user->save(); echo $user->id; return json($data); } public function mozeng()//模型增加 { //第一种是实例化模型对象后赋值并保存: $user = new Attr; $user->name = 'thinkphp'; $user->color = 'thinkphp@qq.com'; $data= $user->save(); $user = new Attr; $data=$user->save([ 'name' => 'thinkphp', 'color' => 'thinkphp@qq.com', 'create_time'=>time() ]); //通过外部 post传值提交赋值给模型,并且希望指定某些字段写入,可以使用: $user = new Attr; // // post数组中只有name和color字段会写入 $user->allowField(['name','color'])->save($_POST); // // || 或者下面 $user = new Attr; // // 过滤post数组中的非数据表字段数据 $data = Request::only(['name','email']); $user->save($data); $user = new Attr; $user->name = 'thinkphp'; $user->color = 'thinkphp@qq.com'; $user->create_time = time(); $data= $user->replace()->save(); //获取自增ID $user = new Attr; $user->name = 'thinkphp'; $user->color = 'thinkphp@qq.com'; $user->create_time= time(); $data = $user->save(); echo $user->id; //批量新增 $user = new Attr; $list = [ ['name'=>'thinkphp','color'=>'thinkphp@qq.com','create_time'=>time()], ['name'=>'onethink','color'=>'onethink@qq.com','create_time'=>time()], ]; $data = $user->saveAll($list); //还可以直接静态调用create方法创建并写入: $user = Attr::create([ 'name' => 'thinkphp', 'color' => 'thinkphp@qq.com', 'create_time'=>time() ]); echo $user->name."<br/>"; echo $user->color."<br/>"; echo $user->id; //获取自增ID $user = Attr::create([ 'name' => 'thinkphp', 'color' => 'thinkphp@qq.com', 'create_time'=>time() ], ['name', 'color','create_time']); echo $user->name."<br/>"; echo $user->color."<br/>"; echo $user->id."<br/>"; // 获取自增ID echo $user->create_time; $user = User::create([ 'name' => 'thinkphp', 'email' => 'thinkphp@qq.com' ], ['name','email'], true); // var_dump($data); boolean 类型 return json($data); //boolean 类型 }