Home > Article > PHP Framework > How to update data in Thinkphp5 model
This article pays attention to the method of updating data of Thinkphp5 model. There are two methods of updating data of thinkphp5 model. I hope it will be helpful to friends who are learning thinkphp!
Thinkphp5 model’s method of updating data
ThinPHP5 model’s two methods of updating data are update, one is the save method, see the actual case code below.
<?php namespace app\index\controller; use think\Controller; use app\index\model\User;//调用模型 class Index extends Controller{ public function index(){ //update一般方法 $res=User::update([ 'id'=>1, 'name'=>'lei' ]); //update参数方法 $res=User::update([ 'id'=>1, 'name'=>'lei' ],['id'=>2]); //update闭包函数方法 $res=User::update([ 'name'=>'lei' ],function($query){ $query->where("id","lt","3"); }); //update where方法 推荐使用的方法 $res=User::where("id","<",6) ->update([ 'name'=>'lei' ]); //save方法 $userModel=User::get(1); $userModel->name='1234'; $res=$userModel->save(); //new save方法 $userModel=new User; $res=$userModel->save([ 'name'=>'lei' ],['id'=>1]); //new save闭包函数方法,次要推荐 $userModel=new User; $res=$userModel->save([ 'name'=>'lei' ],function($query){ $query->where("id","<","5"); }); //saveAll批量更新方法 $userModel=new User; $res=$userModel->saveAll([ ['id'=>1,'name'='lei1'], ['id'=>2,'name'='lei2'] ]); dump($res); } }
PHP Chinese website, a large number of ThinkPHP tutorials, welcome to learn!
The above is the detailed content of How to update data in Thinkphp5 model. For more information, please follow other related articles on the PHP Chinese website!