Laravel提供了
DB facade(原始查找)
查询构造器
Eloquent ORM
三种操作数据库方式
DB facade(原始查找)】
使用DB facade实现CURD
//查询
$student = DB::select('select * from student');
echo '<pre>';
print_r($student);
//添加
$bool = DB::insert('insert into student(name,age) values(?,?)',['sean1',28]);
var_dump($bool);
//修改 返回修改行数
$number = DB::update('update student set age=? where id=?',[20,2]);
var_dump($number);
//删除 返回修改行数
$number = DB::delete('delete from student where id=?',[3]);
var_dump($number);
}
public function test1(){
// //查询
// $student = DB::select('select * from student');
// echo '<pre>';
// print_r($student);
//
// //添加
// $bool = DB::insert('insert into student(name,age) values(?,?)',['sean1',28]);
// var_dump($bool);
// //修改 返回修改行数
// $number = DB::update('update student set age=? where id=?',[20,2]);
// var_dump($number);
//删除 返回修改行数
$number = DB::delete('delete from student where id=?',[3]);
var_dump($number);
}
public function query1(){
// //增
// $bool = DB::table('student')->insert(
// ['name'=>'xxx','age'=>3]
// );
//
// // $id = DB::table("student")->insertGetId();
// $id2 = DB::table("student")->insertGetId(
// ['name'=>'xxx','age'=>3]
// );
//
// echo '<br>'.$id2;
//
$bool = DB::table('student')->insert(
[
['name'=>'xxx1','age'=>3],
['name'=>'xxx2','age'=>3],
['name'=>'xxx3','age'=>3]
]
);
var_dump($bool);
}
public function query2(){
// $bool = DB::table('student')
// ->where('id',8)
// ->update(['age'=>99]);
// var_dump($bool);
$num = $num = DB::table("student")->increment('age',2,['name'=>'aaa']);
var_dump($num);
}
public function query3(){
//返回操作行数
// DB::table("student")
// ->where('id',15)
// ->delete();
// DB::table('student')
// ->where('id','>=',13)
// ->delete();
// DB::talbe->truncate();//无返回值
echo 3333;
}
public function query4(){
//$res = DB::table('student')->get();
// dd($res);
// $first = DB::table("student")->first();
// dd($first);
//
// $first = DB::table("student")->first();
// dd($first);
// $res = DB::table("student")->where('id','>=',10)->get();
//
// $res = DB::table("student")->whereRaw('id >= ? and age > ? ',[1,1])->get();
// dd($res);
//pluck
// $res1 = DB::table("student")->whereRaw('id >= ? and age > ? ',[1,1])->pluck('id','name');
// $res2 = DB::table("student")->whereRaw('id >= ? and age > ? ',[1,1])->pluck('name','id');
//$names = DB::table("student")->select('id','name','age')->get();
// echo '<pre>';
// $names = DB::table("student")->chunk(2,function($student){
// var_dump($student);
// return false;
// });
// dd($names);
// 聚合函数
/*
* count() max() min() avg() sum()
*/
// $res = DB::table('student')->count();
// $res = DB::table('student')->max('age');
// $res = DB::table('student')->min('age');
// $res = DB::table('student')->avg('age');
// $res = DB::table('student')->sum('age');
//
// echo $res;
}
public function orm1(){
// //all()
// $student = Student::all();
//
// //find
// $student = Student::find(1);
//
// //findOrFail
// $student = Student::findOrFail(1111);
// $res = Student::get();
// $res = Student::where('id','>','10')
// ->orderBy('age','desc')
// ->first();
// echo '<pre>';
// $res = Student::chunk(2,function($student){
// var_dump($student);
// });
/// $res =Student::count();
// $res =Student::max('age');
// $res =Student::min('age');
// $res =Student::avg('age');
// $res =Student::sum('age');
// var_dump($res);
//模型新增数据
// $s = new Student();
// $s->name = 'yule';
// $s->age = 65;
// $s->save();
//
// $stu = Student::find(22);
// echo date('Y-m-d H:i:s',$stu->created_at);
// Student::create(
// ['name'=>'immoc','age'=>18]
// );
// Student::firstOrCreate([
// 'age'=>19,
// 'name'=>'hui'
// ]);
// $s = Student::firstOrNew([
// 'age'=>19,
// 'name'=>'hui1'
// ]);
// $s->save();
// dd($s);
// $stu = Student::find(2);
// echo $stu->name;
// $stu->name = 'hello kitty';
// $stu->save();
//$num = Student::where('id','>','20')->update(
// ['age'=>50]
// );
//echo $num;
// $student = Student::find(28);
// $bool =$student->delete();
// var_dump($bool);
// Student::destroy(29);
// echo $num = Student::destroy([24,25,29]);
echo Student::where('id','>',10)->delete();
}