namespaceApp\Http\Controllers;useIlluminate\Support\Facades\DB; classStudentControllerextendsController {//publicfunctiontest1(){//query$student=DB::select('select name,age from student');//insert$bool=DB::insert("insert into student(name, age) values(?, ?)",["imooc",18]);//update$num=DB::update('update student set age = ? where name = ?',[20,'sean']);//delete$num=DB::delete("delete from student where name = ?",["sean"]);//占位符$res=DB::selectOne("select * from class where id = :id",[':id'=>5]);dump($student);} }
使用构造查询器
sql语句如下:CREATE TABLE student ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(255) DEFAULT NULL, age TINYINT(3) DEFAULT NULL, sex TINYINT(3) DEFAULT NULL, create_at INT(11) DEFAULT NULL, update_at INT(11) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=MYISAM DEFAULT CHARSET=utf8;
publicfunctionquery1(){//insert$res=DB::table("student")->insert(['name'=>'imooc','age'=>18]);//insertGetId$id=DB::table("student")->insertGetId(['name'=>'imooc1','age'=>18]);//insert multi rowsDB::table("student")->insert([['name'=>'imooc1','age'=>18],['name'=>'imooc2','age'=>18]]);var_dump($id);//int(4)}publicfunctionquery2(){//update$num=DB::table("student")->where("id",4)->update(['age'=>30]);//increment and decrement$num=DB::table('student')->increment('age',3);$num=DB::table('student')->where("id",4)->decrement('age',3,['name'=>'test']);}publicfunctionquery3(){//deleteDB::table("student")->where("id",">=",3)->delete();DB::table("student")->truncate();}publicfunctionquery4(){//get all$students=DB::table("student")->get();//get the first item of query result$student=DB::table("student")->orderBy("id","desc")->first();//orWhere$student=DB::table("student")->where("id",4)->orWhere("name",'tom')->first();//闭包查询$kw=$request->get("kw");//如果$kw为真则执行匿名函数里的查询$res=DB::table('class')->when($kw,function(Builder $query)use($kw){$query->where('pid','like',"%{$kw}%");})->get();//多条件查询$students=DB::table("student")->whereRaw("id >= ? and age > ?",[5,20])->get();//pluck (返回指定的字段)$names=DB::table("student")->pluck("name");//lists (效果同pluck) 但可以指定第二个参数值为下标$list=DB::table("student")->lists("name","id");//select 同tp5中的column$names=DB::table("student")->select("id","name",'age')->get();//chunk 用法同tp5DB::table("student")->chunk(2,function($students){var_dump($students);});}publicfunctionquery5(){$num=DB::table("student")->count();DB::table("student")->max("age");DB::table("student")->min("age");DB::table("student")->sum("age");var_dump($num);}
publicfunctionorm1(){//get all the items$all= Student::all();//get one by pk [TP5 get]$one= Student::find(1000);// findOrFail() will throw an error if the res does not exist$one= Student::findOrFail(3000);// query by conditionStudent::where("id",">",3)->orderBy("id","desc")->first();Student::chunk(2,function($students){//pass});Student::max("id");}publicfunctionorm2(){//if save failed add public $timestamps = false in the model // $student = new Student(); // $student->name = "sean"; // $student->age = 20; // $bool = $student->save();//插入的字段需要在模型$fillable中设定$student= Student::create(['name'=>'imooc','age'=>18]);$data=['name'=>'tom','age'=>18];//insert插入,不会自动设置timestampStudent::insert($data);//以属性查找,如没有则新增Student::firstOrCreate(["name"=>"imoocfirst"]);//以属性查找,如果没有则新增实例$s= Student::firstOrNew(["name"=>"imoocnew"]);$s->save();}publicfunctionorm3(){//update by model$student= Student::find(4);$student->name="update orm3";$bool=$student->save();$num= Student::where("id",">",4)->update(['age'=>41]);var_dump($num);}publicfunctionorm4(){//delete//1. // $student = Student::find(4); // $num = $student->delete();//2. by pk$num= Student::destroy(15,16);//3. by conditionStudent::where("id",">",104)->delete();}