laravel-数据添加和数据更新及数据删除
工具/原料
laravel
数据添加
1
1.插入单行,一维数组形式,数组的键就是表中字段,返回值为true 和 false。代码:$row = ['titles'=>'测试数据', 'email'=>''];DB::table('goods')->insert($row);
2
2.效果:
3
3.插入多行,二维数组形式,二维数组中单元的键是表中字段,返回值为true 和 false。 array('titles'=>'测试数据1', 'email'=>''), array('titles'=>'测试数据2', 'email'=>''));DB::table('goods')->insert($rows);
4
4.插入后返回主键值,获取主键值用insertGetId()方法(但是需要注意的是对多维数组不行)$row = ['titles'=>'测试数据', 'email'=>''];$id = DB::table('goods')->insertGetId($row);var_dump($id);
5
5.效果:
数据更新
1.在laravel中对数据的修改如下:DB::table('tableName')->where('id', 1)->update(['age' => 19]);对于上面的操作相当于sql:update tableName set age=19 where id=1 ;
数据删除
1
1.在laravel中对数据的修改如下:DB::table(‘tableName’)->where('id' , '>' , 3)->delete();备注:当where方法有三个参数时,其中第二个参数当做运算符,并且返回值是受影响的行数。
2
2.效果: