Thinkphp5模型添加数据的方法

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

Thinkphp5模型添加数据的方法

Thinkphp5模型添加数据的方法

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

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

推荐学习:thinkphp教程

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享