laravel5.6 數據庫操作-查詢構建器
1 <?php 2 //laravel5.6 語法 demo示例 3 4 namespace App\Http\Controllers;//命名該控制App空間下名稱 5 6 use Illuminate\Support\Facades\DB;//使用DB操作數據庫 7 use App\Http\Controllers\Controller;//繼承基礎控制器 8 9 class UserController extends Controller 10 { 11 /** 12 * 展示應用的用戶列表. 13 * 14 * @return Response 15 */ 16 public function index() 17 { 18 //DB使用為每種操作提供了相應方法:select(查),update(修改),insert(插入),delete(刪除),statement(聲明) 19 //建議占位符,其他框架通用性強 20 //原生sql寫法 21 $data = DB::select('select * from users where id = :id and name = :name ',[':id' => 1,':name' =>'測試']); 22 23 //查方法 24 //get() 方法獲取表中所有記錄(獲取多行多列) 25 $data = DB::table('users')->get(); 26 27 //first() 方法將會返回單個對象(獲取一行一列) 28 //where() 方法查詢指定條件對象 29 $data = DB::table('users')->where('id','name','3','測試')->first(); 30 31 //select() 方法可以查詢指定自定義字段 32 $data = DB::table('users')->select('id','name', 'email')->get(); 33 34 //value() 方法從結果中獲取單個值,該方法會直接返回指定列的值: 35 $data = DB::table('users')->where('name','測試')->value('email'); 36 37 //pluck() 方法獲取單個列值的數組 38 $data = DB::table('users')->pluck('name'); 39 40 //count() 統計數量 41 $data = DB::table('users')->count(); 42 43 //exists() 方法來判斷匹配查詢條件的結果是否存在 44 $data=DB::table('users')->where('id', 1)->exists(); 45 46 //join() 方法連表查詢 47 $data = DB::table('users') 48 ->join('ceshi', 'users.id', '=', 'ceshi.id') 49 ->select('users.*', 'ceshi.name') 50 ->get(); 51 52 //leftJoin() 方法左連表查詢 53 $data = DB::table('users') 54 ->leftJoin('ceshi', 'users.id', '=', 'ceshi.id') 55 ->select('users.*', 'ceshi.name') 56 ->get(); 57 58 //where() 參數說明:(一)參數是列名,(二)參數是操作符,(三)參數是該列要比較的值 59 $data = DB::table('users') 60 ->where('id', '>=', 1) 61 ->where('name', 'like', '測試%') 62 ->get(); 63 64 //傳遞條件數組到where中寫法,建議多where查詢使用這個方法 65 $data = DB::table('users') 66 ->where([ 67 ['id', '>=', 1], 68 ['name', 'like', '測試%'] 69 ]) 70 ->get(); 71 72 //whereBetween() 方法驗證列值是否在給定值之間 73 $data = DB::table('users') 74 ->whereBetween('id', [1, 3])->get(); 75 76 //whereIn 方法驗證給定列的值是否在給定數組中: 77 $data = DB::table('users') 78 ->whereIn('id', [1, 2, 3]) 79 ->get(); 80 81 //orderBy() 方法排序 82 $data = DB::table('users') 83 ->orderBy('id', 'desc') 84 ->get(); 85 86 //insert() 方法插入記錄到數據表 87 //insertGetId() 方法插入記錄並返回自增ID值 88 $data=DB::table('users')->insert( 89 [ 90 'name'=>'測試', 91 'email' => 'ceshi.com', 92 'password' => 'ceshi' 93 ] 94 ); 95 96 //update() 方法修改記錄 97 $data =DB::table('users') 98 ->where('id', 1) 99 ->update(['name' => '測試']); 100 101 //delete() 方法刪除記錄 102 $data=DB::table('users')->where('id', '>', 10)->delete(); 103 104 105 //paginate() 方法分頁 每頁顯示數量 106 //注意:目前使用 groupBy 的分頁操作不能被Laravel有效執行 107 $data = DB::table('users')->paginate(2); 108 109 110 //前台分頁中鏈接附加參數實現分頁 111 $getName = $GET['name']?:''; 112 $data = DB::table('users') 113 ->select('id','name','age') 114 ->where('name', 'like', $getName.'%') 115 ->paginate(2); 116 117 //返回給前端視圖數據 118 return $this->view('index',['data'=>$data,'namePage'=>$getName]); 119 120 //前端引用代碼 121 //appends 方法添加查詢參數到分頁鏈接查詢字符串; 添加 &name=$namePage到每個分頁鏈接中. 122 {{ $data->appends(['name' => $namePage])->links() }} 123 124 125 //simplePaginate() 方法分頁視圖中簡單的顯示“下一頁”和“上一頁”鏈接 126 $data = DB::table('users')->simplePaginate(2); 127 //返回給前端視圖數據 128 return $this->view('index',['data'=>$data]); 129 //前端簡單引用代碼 130 <div class="container"> 131 @foreach ($users as $user) 132 {{ $user->name }} 133 @endforeach 134 </div> 135 {{ $data->links() }} 136 137 138 //原生分頁寫法 139 $page = 2; 140 $pageSize = 1; 141 $offset = ($page - 1) * $pageSize; 142 $result = DB::table('picasa') 143 ->where('title', 'like', '%'.$title.'%') 144 ->offset($offset) 145 ->limit($pageSize) 146 ->get(); 147 148 //返回數據視圖文件 149 return $this->view('index', ['result' => $result]); 150 151 } 152 }
groupBy 對查詢結果進行分組出現問題
1 當select和groupBy中列表不一致時候會報錯。mysql從5.7以后,默認開啟group by的嚴格模式。 2 3 解決方法:找到config/database.php 在mysql下面把'strict' => true,改為false。[建議不要修改。寫對正確操作語法。] 4 5 例如: 6 $booked = DB::table('booked_user') 7 ->select('game_id', DB::raw('count(*) as total')) 8 ->groupBy('game_id') 9 ->get();
開啟sql查詢日志
1 DB::connection()->enableQueryLog();//開啟QueryLog 2 $data = DB::table('users')->select('id','name', 'email')->get();//執行sql 3 dump(DB::getQueryLog());//sql語句和查詢時間
寫入日志信息
八種日志級別:emergency、alert、critical、error、warning、 notice、info 和 debug
默認日志存放位置: /storage/logs/laravel.log
引用: use Illuminate\Support\Facades\Log;
1 Log::emergency(string $message, array $context = []); 2 Log::alert(string $message, array $context = []); 3 Log::critical(string $message, array $context = []); 4 Log::error(string $message, array $context = []); 5 Log::warning(string $message, array $context = []); 6 Log::notice(string $message, array $context = []); 7 Log::info(string $message, array $context = []); 8 Log::debug(string $message, array $context = []);
laravel5.6 操作數據ORM鏈接: 請點擊跳轉