您的位置 首页 技术

Thinkphp5模型添加数据的方法

本篇文章介绍了thinkphp5中模型添加数据的两种方法,希望对学习thinkphp的朋友有帮助! Thinkphp5模型添加数据的方法 thinPHP5模型添加数据的方法有两个一…

本篇文章介绍了thinkphp5中模型添加数据的两种方法,希望对学习thinkphp的朋友有帮助!

Thinkphp5模型添加数据的方法

thinPHP5模型添加数据的方法有两个一个是create,一个是save方法,下面看实际案例代码。

<?phpnamespace app\index\controller;use think\Controller;use app\index\model\User;public function index(){    //create方法添加数据    $res=User::create([      'name'=>'lei',      'email'=>'leixiaotian@163.com',      'password'=>'123'    ],true);//true排除掉表中不存在的字段    dump($res->id);    dump($res);    //save方法添加    $userModel=new User;    $userModel->name='lei';    $userModel->email='leixiaotian@163.com';    $userModel->save();    dump($userModel->id);    //sava数组方法    $res=$userModel->save([      'name'=>'lei',      'email'=>'leixioatian@163.com'    ]);    dump($res);    //sava允许字段方法    $userModel=new User;    $res=$userModel    ->allowField(['name'])    ->save([      'name'=>'lei',      'email'=>'leixioatian@163.com'    ]);    dump($res);    //sava保存多条数据    $userModel=new User;    $res=$userModel->saveAll([      ['name'=>'lei'],      ['name'=>'lei']    ]);    foreach ($res as $val) {      dump($val->toArray());    }  } }

推荐学习:thinkphp教程

以上就是Thinkphp5模型添加数据的方法的详细内容,更多请关注24课堂在线网其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/52687.html

为您推荐

返回顶部