laravel 查询构造器

//查询构造器
public function query()
{
$bool = DB::table('student')->insert([
['name' => '王五', 'age' => 17, 'gender' => 1],
['name' => '王五2', 'age' => 17, 'gender' => 1]
]);
var_dump($bool);

//获取插入的数据的id
$student_id = DB::table('student')->insertGetId(
['name' => '赵六', 'age' => 19, 'gender' => 2]
);
var_dump($student_id);


//更新数据,返回影响的行数
$num = DB::table('student')->where('id',1)->update(
['age' => 20, 'gender' => 2]
);
var_dump($num);

//更新数据,自增字段,返回影响的行数
$num = DB::table('student')->where('id',1)->increment('age');//自增1
$num = DB::table('student')->where('id',1)->increment('age',3);//自增3
$num = DB::table('student')->where('id',1)->decrement('age',3);//自减3
var_dump($num);

//更新数据,自增以及其他字段修改
$num = DB::table('student')->where('id',1)->increment('age',3,['name'=>'haha']);

//删除数据,返回删除的行数
$num = DB::table('student')->where('id',1)->delete();
$num = DB::table('student')->where('id','>=',10)->delete();

//清空数据,不返回任何东西
$num = DB::table('student')->truncate();

}
posted @ 2018-05-14 18:00  学知无涯  阅读(178)  评论(0编辑  收藏  举报