laravel 數據庫操做之 DB facade & 查詢構造器 & Eloquent ORM

<?php

namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;

class StudentController extends Controller
{
  //DB facade原始SQL語句
public function test1() {
     $students = DB::select('select * from student'); //var_dump($students); dd($students); }

 
  //查詢構造器新增數據-增
    public function query1()
    {
        //普通插入
//        $bool = DB::table('student')->insert(
//            ['name' => 'imooc', 'age'=> 20]
//        );
//        dd($bool);

        //返回自增加id
//        $id = DB::table('student')->insertGetId(
//            ['name' => 'cxll', 'age'=> 18]
//        );
//        var_dump($id);
     //批量插入 $bool = DB::table('student')->insert([ ['name'=>'name1', 'age' =>20], ['name'=>'name2', 'age' =>19] ]); dd($bool); } //查詢構造器更新數據-改 public function query2() { //返回影響行數 // $num = DB::table('student') // ->where('id', 2) // ->update(['age'=>30]); // dd($num); //自增減 默認1 //$num = DB::table('student')->increment('age'); //$num = DB::table('student')->increment('age',3); //$num = DB::table('student')->decrement('age',5); // $num = DB::table('student') // ->where('id', 3) // ->decrement('age');
    //自增同時更新其它數據 $num = DB::table('student') ->where('id', 3) ->increment('age',2,['name'=> 'newname']); dd($num); } //查詢構造器刪除數據-刪 public function query3() { //返回操做影響的行數 // $num = DB::table('student') // ->where('id',4) // ->delete(); // $num = DB::table('student') // ->where('id','>',2) // ->delete(); // dd($num); //清空表 無返回值 DB::table('student')->truncate(); }
  //查詢構造器查詢數據-查
public function query4(){ // $bool = DB::table('student') ->insert([ // ['id' => 1001, 'name' => 'name1', 'age' =>18], // ['id' => 1002, 'name' => 'name2', 'age' =>19], // ['id' => 1003, 'name' => 'name3', 'age' =>18], // ['id' => 1004, 'name' => 'name4', 'age' =>21], // ['id' => 1005, 'name' => 'name5', 'age' =>18] // ]); // dd($bool); //get() //$students = DB::table('student')->get(); //first() // $student = DB::table('student') // ->orderBy('id', 'desc') // ->first(); // dd($student); //where() // $students = DB::table('student') // ->where('id', '>=', 1002) // ->get(); // $students = DB::table('student') // ->whereRaw('id >= ? and age > ?', [1001, 18]) // ->get(); // dd($students); //pluck() 取字段 // $names = DB::table('student') // ->pluck('name'); //指定下標爲id // $names = DB::table('student') // ->pluck('name','id'); //lists() laravel5.5不支持lists了, 被pluck取代 //$names = DB::table('student')->lists('name','id'); //select() 指定查找 // $names = DB::table('student') // ->select('id', 'name') // ->get(); // dd($names); //chunk() 分段獲取 需指定排序字段 echo '<pre>'; DB::table('student')->orderBy('id') ->chunk(2, function($students){ var_dump($students); }); } //聚合函數 public function query5() { //$num = DB::table('student')->count(); //$max = DB::table('student')->max('age'); //$min = DB::table('student')->min('age'); //$avg = DB::table('student')->avg('age'); $sum = DB::table('student')->sum('age'); dd($sum); }


//使用orm查詢數據-查 public function orm1() { //all() //$students = Student::all(); //find() //$student = Student::find(1001); //findOrFail() //$student = Student::findOrFail(1001); //dd($student); //get() //$students = Student::get(); //first() // $student = Student::where('id', '>', '1001') // ->orderBy('age', 'desc') // ->first(); // dd($student); //chunk() // echo '<pre>'; // Student::chunk(2, function($students){ // var_dump($students); // }); //聚合函數 //$num = Student::count(); $max = Student::where('id','>',1001)->max('age'); dd($max); } //使用orm新增數據-增 public function orm2() { //使用模型新增數據 // $student = new Student(); // $student->name = 'sean2'; // $student->age = 20; // //存入數據庫 // $bool = $student->save(); // dd($bool); //$student = Student::find(1010); //dd($student->created_at); //將時間戳按格式輸出 //echo date('Y-m-d H:i:s', 1464509164); //使用模型的Create方法新增數據 // $student = Student::create( // ['name' => 'imooc2', 'age' => 19] // ); // dd($student); //firstOrCreate() 查不到就新增至數據庫 // $student = Student::firstOrCreate( // ['name'=>'imooc3'], // ['age'=>18] // ); //firstOrNew() 查不到就建立實例,不主動插入數據庫 $student = Student::firstOrNew( ['name'=>'imooc4'], ['age'=>18] ); $student->save(); dd($student); }
//使用orm更新數據-改 public function orm3() { //經過模型更新數據 // $student = Student::find(1014); // $student->name = 'kitty'; // $bool = $student->save(); // dd($bool); $num = Student::where('id', '>', 1012)->update( ['age'=>41] ); dd($num); } //使用orm刪除數據-刪 public function orm4() { //經過模型刪除 // $student = Student::find(1014); // $bool = $student->delete(); // dd($bool); //經過主鍵刪除 // $num = Student::destroy(1013); // $num = Student::destroy(1012,1013); // $num = Student::destroy([1012,1013]); // dd($num); //刪除指定條件的數據 $num = Student::where('id', '>', 1008)->delete(); dd($num); } }

Student 控制器類👆php

Student 模型類👇laravel

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Student extends Model{
//每一個模型對應一個數據表,用於交互

    //指定表名
    protected $table = 'student';

    //指定主鍵
    protected $primaryKey = 'id';

    //指定容許批量賦值的字段
    protected $fillable = ['name', 'age'];

    //指定不容許批量賦值的字段
    protected $guarded = [];

    public static function getStudent(){
        return 'student is sean';
    }



    //自動維護時間戳
   // public $timestamps = true;

    //針對時間戳 將其轉換爲標準日期格式
//    protected function getDateFormat()
//    {
//        return time();
//    }

    //針對時間戳  不作格式處理
//    protected function asDateTime($val)
//    {
//        return $val;
//    }

}

 

👉Reference: 輕鬆學會Laravel-基礎篇數據庫

相關文章
相關標籤/搜索