Home > PHP Framework > ThinkPHP > body text

How to update data in Thinkphp5 model

angryTom
Release: 2020-03-17 09:13:55
forward
4007 people have browsed it

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!

How to update data in Thinkphp5 model

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.

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);
  }
 }
Copy after login

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!

Related labels:
source:www.100txy.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!