慕虎4711321
2018-10-07 17:07
public function add() {
// $result = DB::insert('insert into student(name, age) values(?, ?)', ['xxx', 18]);
// var_dump($result);
$result = DB::table('student')
->insertGetId(['name' => 'mmm', 'age'=>20]);
var_dump($result);
return "StudentController - add";
}我使用 两种方式插入数据, 每次访问一次接口,数据库总是插入两条相同的数据,除了 主键外,其他都相同,是为什么呢?
mysql> select * from student;
+-----+------+-----+-----+------------+------------+
| id | name | age | sex | created_at | updated_at |
+-----+------+-----+-----+------------+------------+
| 108 | mmm | 20 | 0 | 0 | 0 |
| 109 | mmm | 20 | 0 | 0 | 0 |
+-----+------+-----+-----+------------+------------+
你这里添加一次了 $result = DB::table('student') ->insertGetId(['name' => 'mmm', 'age'=>20]);
return "StudentController - add";这里又一次;所以是两次轻松学会Laravel-基础篇
64318 学习 · 298 问题
相似问题