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

Thinkphp5模型添加数据的方法
thinPHP5模型添加数据的方法有两个一个是create,一个是save方法,下面看实际案例代码。
<?php
namespace 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模型添加数据的方法的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号