Laravel資料庫操作之-增刪改查CURD操作
阿新 • • 發佈:2019-02-02
資料庫增刪改查CURD操作
// 新增資料 返回bool值
// $bool = DB::insert('insert into student(name,age) values(?,?)',['imooc',19]);
// var_dump($bool);
// 更新資料
// $num = DB::update('update student set age = ? where name = ?',[20,'sean']);
// var_dump($num);
// 查詢資料
// $students = DB::select('select * from student where id > ?',[1001 ]);
// dd($students);
// 刪除資料
$num = DB::delete('delete from student where id > ?',[1001]);
var_dump($num);
資料庫操作之-查詢構造器
- 查詢構造器簡介及新增資料
- 使用查詢構造器修改資料
- 使用查詢構造器刪除資料
- 使用查詢構造器查詢資料
- 查詢構造器中的聚合函式
1. 查詢構造器簡介
- laravel查詢構造器(query builder)提供方便、流暢的介面,用來建立執行資料庫查詢語法
- 使用PDO引數繫結,以保護應用程式免於SQL注入因此傳入的引數不需額外轉義特殊字元
- 基本上可以滿足所有的資料庫操作,而且在所有支援的資料庫系統上都可以執行
1. 使用查詢構造器新增資料
// 插入資料 返回插入資料的bool值
// $bool = DB::table('student')->insert(
// ['name'=>'imooc','age'=>18]
// );
// var_dump($bool);
// 插入資料 返回插入資料的id
// $id = DB::table('student')->insertGetId(
// ['name'=>'sean','age'=>18]
// );
// var_dump($id);
// 插入多條資料
$bool = DB::table('student')-> insert(
[
['name'=>'name1','age'=>20],
['name'=>'name2','age'=>21],
]
);
var_dump($bool);
2. 使用查詢構造器更新資料
- 更新為指定的內容
- 自增和自減
// 更新資料,返回受影響行數
// $num = DB::table('student')
// ->where('id',1003)
// ->update(['age'=>30]);
// var_dump($num);
// 自增 預設自增1 將所有的age自增1
// $num = DB::table('student')->increment('age');
// 自增 將所有的age自增3
// $num = DB::table('student')->increment('age',3);
// 自減 將所有的age自減3
// $num = DB::table('student')->decrement('age',3);
// 自減 預設自減1 將所有的age自減1
// $num = DB::table('student')->decrement('age');
// 將id為1003的age自減3
// $num = DB::table('student')
// ->where('id',1003)
// ->decrement('age',3);
// 自減的同時,修改其他欄位
$num = DB::table('student')
->where('id',1003)
->decrement('age',3,['name'=>'iimooc']);
var_dump($num);
3. 使用查詢構造器刪除資料
- delete
- truncate
// $num = DB::table('student')
// ->where('id',1006)
// ->delete();
// $num = DB::table('student')
// ->where('id','>=',1003)
// ->delete();
// var_dump($num);
// 不返回任何東西 刪除所有資料
DB::table('student')->truncate();
4. 使用查詢構造器查詢資料
- get()
- first()
- where()
- pluck()
- lists()
- select()
- chunk()
// 插入資料 返回插入資料的bool值
// $bool = DB::table('student')->insert([
// ['id'=>1001,'name'=>'name1','age'=>18],
// ['id'=>1002,'name'=>'name2','age'=>18],
// ['id'=>1003,'name'=>'name3','age'=>19],
// ['id'=>1004,'name'=>'name4','age'=>20],
// ['id'=>1005,'name'=>'name5','age'=>21]
// ]);
// var_dump($bool);
// get() 獲取表的所有資料
// $students = DB::table('student')->get();
// first() 獲取結果集中的第一條資料
// $student = DB::table('student')
// ->orderBy('id','desc')
// ->first();
// where
// $students = DB::table('student')
// ->where('id','>=',1002)
// ->get();
// 多個條件
// $students = DB::table('student')
// ->whereRaw('id >= ? and age > ?',[1001,18])
// ->get();
// pluck 返回結果集中的欄位
// $names = DB::table('student')
// ->pluck('name');
// lists 返回結果集中的欄位 指定某個欄位做下標
// $names = DB::table('student')
// ->lists('name','id');
// select
// $selects = DB::table('student')
// ->select('id','name','age')
// ->get();
// dd($selects);
// chunk
echo '<pre>';
DB::table('student')->chunk(2,function($students){
var_dump($students);
if (true) {
return false;
}
});
5. 使用查詢構造器中的聚合函式
- count()
- max()
- min()
- avg()
- sum()
// $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');
var_dump($sum);