二.数据库操作相关知识要点:
第12集 使用DB Facade实现数据库操作
第13集 查询构造器-新增数据
第14集 查询构造器-删除数据
第15集 查询构造器-更新数据
第16集 查询构造器-查询数据
<?php /** * Created by PhpStorm. * User: TXDK * Date: 2020-11-23 * Time: 16:01 */ namespace App\Http\Controllers; use App\Models\Student; use Illuminate\Support\Facades\DB; class StudentController { public function showStudent(){ $studentInfo = Student::studentInfo(); return view("student", ["studentName"=> $studentInfo, ]); } public function dbFacade(){ $bool = DB::insert('insert into student(name, age, score) values (?, ?, ?)', ['tony', 7, 100]); var_dump($bool); $bool = DB::update('update student set age=20 where name=?', ['tony']); var_dump($bool); $bool = DB::delete("delete from student where name=?", ["tony"]); var_dump($bool); $studentInfo = DB::select("select * from student"); dd($studentInfo); foreach ($studentInfo as $student){ echo "姓名:" . $student->name ."<br/>" . "年龄:" . $student->age . "<br/>分数:" . $student->score . "<br/>"; } $studentInfo = DB::select("select * from student where name=:name", ["name"=>"snow"]); dd($studentInfo); } public function builder(){ $bool = DB::table("student")->insert(["name"=>"alisa", "age"=>"13", "score"=>88]); var_dump($bool); $bool = DB::table("student")->insert([ ["name"=>"jacker", "age"=>"17", "score"=>85], ["name"=>"rose", "age"=>"33", "score"=>80] ]); var_dump($bool); $id = DB::table("student")->insertOrIgnore(["id"=>7 ,"name"=>"baker", "age"=>"13", "score"=>88]); var_dump($id); $n = DB::table("student")->where('name', "rose")->update(["age"=>21]); var_dump($n); $n = DB::table("student")->updateOrInsert( ["name"=>"tony"], ["age"=>18, "score"=>66] ); var_dump($n); $n = DB::table("student")->where("name", "tony")->increment("age", 5); var_dump($n); $results = DB::table("student")->where("score", ">" , 90)->get(); $results = DB::table("student") ->whereRaw("age > ? and score > ?", [20, 80]) ->get(); foreach ($results as $item){ echo "姓名:" . $item->name . "|年龄:" . $item->age . "|分数:" . $item->score . "</br><hr>"; } $results = DB::table("student") ->where("name", "tony")->value("age"); echo $results; $item = DB::table("student") ->find(2); echo "姓名:" . $item->name . "|年龄:" . $item->age . "|分数:" . $item->score . "</br><hr>"; $items = DB::table("Student")->orderBy("id", 'desc')->pluck("name"); echo $items; echo "<pre>"; $items = DB::table("student")->orderBy("id", 'desc')->chunk(2, function ($students){ //var_dump($students); foreach ($students as $student){ echo "<br/>姓名:" . $student->name . "年龄:" . $student->age . "分数:" . $student->score; if ($student->name == "tony"){ return false; } } }); $n = DB::table("student")->count(); echo $n; $max_age = DB::table("student")->where("score", ">=", 80)->min("age"); echo $max_age; $results = DB::table("student")->where("score", ">=", 80)->select("name", "age")->get(); dd($results); }
public function orm(){ $student = new Student(); $student->name = "jony"; $student->age = 7; $student->score = 88; $res = $student->save(); dd($res); $student = Student::create( ["name"=>"rose", "age"=>18, "score"=>"78"] ); $student = Student::firstOrCreate( ["name"=>"snow", "age"=>38, "score"=>100] ); dd($student); $res = Student::get(); $res->reject(function ($res){ var_dump($res->name); }); dd($res); $res = Student::first(); dd($res); echo "<pre>"; Student::chunk(2, function ($item){ var_dump($item); }); echo "<pre>"; foreach ( Student::cursor() as $item){ var_dump( $item->name); } $n = Student::count(); echo $n; $item = Student::where("age", ">", 30) ->select("name", "age")->get(); dd($item); $item = Student::findOrFail("1"); dd($item); $item = Student::where("age", ">", 100)->firstOrFail(); dd($item); //更新数据 $student = Student::first(); $student->name = "baker"; $student->age = 100; $student->save(); Student::where("name", "=", "baker") ->update(["name"=>"penny", "age"=>"35"]); //删除数据 $student = Student::first(); $student->delete(); Student::destroy(15); Student::where("age", ">=", 18)->delete(); }
}