因?yàn)槭鞘褂貌樵儤?gòu)建器對(duì)數(shù)據(jù)庫(kù)進(jìn)行增刪改查浪读,所以以下都是相關(guān)的方法介紹:
一 新增數(shù)據(jù)
筆者喜歡的固定方法insertGetId()
,插入記錄后返回自增ID挟伙。
$insertId = DB::table('users')->insertGetId(
['name'=>'Laravel-Academy','email'=>'laravelacademy@test.com','password'=>'456']
);
二 更新數(shù)據(jù)
更新表記錄很簡(jiǎn)單害碾,使用查詢構(gòu)建器的update方法,返回兩種狀態(tài):1表示更新完成;0表示更新失敗
$affected = DB::table('users')->where('name','Laravel-Academy')->update(['password'=>'123']);
update()
方法里必須放的是數(shù)組形式赡若。
三 刪除數(shù)據(jù)
使用delete方法刪除表記錄扣溺,刪除方法和更新方法類(lèi)似,返回兩種狀態(tài):1表示刪除成功 0表示刪除失敗
$deleted = DB::table('users')->where('id', '>', 3)->delete();
ps:
如果我們是要?jiǎng)h除整個(gè)數(shù)據(jù)表數(shù)據(jù)镐确,則略去where條件包吝,如果是要清空數(shù)據(jù)表還要將自增ID置為0,可以使用truncate方法:
DB::table('users')->truncate();
四 查詢
4.1 獲取所有表記錄
$users = DB::table('common_emotion')->get();
dd($users);
得到如下所示:
Collection {#345 ▼
#items: array:48 [▼
0 => {#346 ▼
+"id": 1
+"name": "已按期完成了前述超短期融資券的兌付"
+"property": "1"
+"score": 1
+"version": 1.0
}
1 => {#347 ?}
2 => {#348 ?}
3 => {#349 ?}
4.2 如果是獲取指定列的數(shù)據(jù)源葫,則需要加上select條件:
$affected = DB::table('common_emotion')->select('name','score')->get();
dd($affected);
得到如下所示:
Collection {#344 ▼
#items: array:48 [▼
0 => {#345 ▼
+"name": "已按期完成了前述超短期融資券的兌付"
+"score": 1
}
1 => {#347 ▼
+"name": "固定收益類(lèi)有所增加"
+"score": 1
}
4.3 獲取單條記錄需要在查詢基礎(chǔ)上加上first方法:
$affected = DB::table('common_emotion')->first();
dd($affected);
得到:
{#346 ▼
+"id": 1
+"name": "已按期完成了前述超短期融資券的兌付"
+"property": "1"
+"score": 1
+"version": 1.0
}
五 事務(wù)
在涉及到多表操作時(shí)诗越,為了保證數(shù)據(jù)的可靠性和統(tǒng)一性,我們可以加入事務(wù)處理
DB::beginTransaction();
try {
DB::commit();
} catch (Exception $e){
DB::rollback();
throw $e;
}
六 高級(jí)操作
6.1 聚合函數(shù) 比如 count
, max
, min
, avg
, 和 sum
$users = DB::table('users')->count();
$price = DB::table('orders')->max('price');
6.2 where語(yǔ)句
$users = DB::table('users')->where('votes', '>', 100)->get(); //votes字段大于100 的所有結(jié)果集
$users = DB::table('users')->where('votes', 100)->get();//votes字段等于100的所有結(jié)果集
$users = DB::table('users')
->where('votes', '>=', 100)
->get();//votes字段大于等于100的所有結(jié)果集
$users = DB::table('users')
->where('votes', '<>', 100)
->get();//votes字段大于小于100的
$users = DB::table('users')
->where('name', 'like', 'T%')
->get();//模糊查詢
6.3 你可以通過(guò)方法鏈將多個(gè)where約束鏈接到一起息堂,也可以添加or子句到查詢嚷狞,orWhere方法和where方法接收參數(shù)一樣:得到多個(gè)條件約束的并集
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere('name', 'John')
->get();
6.4 whereBetween方法驗(yàn)證列值是否在給定值之間:需要注意的是between里給的數(shù)組里面的值是有順序的,是一個(gè)起始坐標(biāo)值荣堰。例如下面:獲取votes的值從1到100區(qū)間范圍的所有結(jié)果集 不能寫(xiě)出[100,1]
$users = DB::table('users')
->whereBetween('votes', [1, 100])->get();
6.5 whereNotBetween方法驗(yàn)證列值不在給定值之間:和6.3的情況一下床未。
$users = DB::table('users')
->whereNotBetween('votes', [1, 100])
->get();
6.6 whereIn方法驗(yàn)證給定列的值是否在給定數(shù)組中:這里的數(shù)組是沒(méi)有順序的 得到的是 id值在1,2,3中的所有結(jié)果集
$users = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
6.7 whereNotIn方法驗(yàn)證給定列的值不在給定數(shù)組中:同6.5一樣
$users = DB::table('users')
->whereNotIn('id', [1, 2, 3])
->get();
七 排序 分組 限定
7.1 orderBy方法允許你通過(guò)給定列對(duì)結(jié)果集進(jìn)行排序,orderBy的第一個(gè)參數(shù)應(yīng)該是你希望排序的列振坚,第二個(gè)參數(shù)控制著排序的方向——asc或desc:
$users = DB::table('users')
->orderBy('name', 'desc')
->get();
7.2 groupBy和having方法用于對(duì)結(jié)果集進(jìn)行分組薇搁,having方法和where方法的用法類(lèi)似:
$users = DB::table('users')
->groupBy('account_id')
->having('account_id', '>', 100)
->get();
7.3 想要限定查詢返回的結(jié)果集的數(shù)目,或者在查詢中跳過(guò)給定數(shù)目的結(jié)果渡八,可以使用skip和take方法:
$users = DB::table('users')->skip(10)->take(5)->get();
7.4 限制獲取條數(shù)
$users = DB::table('users')->limit(10)->get();
$users = DB::table('users')->offset(0)->limit(10)->get();
從第2個(gè)開(kāi)始啃洋,獲取10個(gè)
7.5 從數(shù)據(jù)庫(kù)中獲取單一數(shù)據(jù)列的單一字段 得到的結(jié)果
$orientation=DB::table('data_yq_list')->where('kvUuid',$idtime[$i][0])->pluck("kvOrientation")->first();//會(huì)得到一個(gè)字符串 “2”
$orientation=DB::table('data_yq_list')->where('kvUuid',$idtime[$i][0])->pluck("kvOrientation");//會(huì)得到一個(gè) collection類(lèi)
八 分頁(yè)查詢
8.1 查詢時(shí) paginate的唯一參數(shù)就是你每頁(yè)想要顯示的數(shù)目屎鳍,這里我們指定每頁(yè)顯示15個(gè):
/**
* 顯示應(yīng)用中的所有用戶
*
* @return Response
*/
public function index()
{
$users = DB::table('users')->where('votes', '>', 100)->paginate(15);
return view('user.index', ['users' => $users]);
}
前端視圖如何獲取?
<div class="container">
@foreach ($users as $user)
{{ $user->name }}
@endforeach
</div>
{!! $users->links() !!}
links方法將會(huì)將結(jié)果集中的其它頁(yè)面鏈接渲染出來(lái)宏娄。每個(gè)鏈接已經(jīng)包含了?page查詢字符串變量
8.2 添加參數(shù)到分頁(yè)鏈接
你可以使用appends方法添加查詢參數(shù)到分頁(yè)鏈接查詢字符串。例如逮壁,要添加&sort=votes到每個(gè)分頁(yè)鏈接孵坚,應(yīng)該像如下方式調(diào)用appends:
{!! $users->appends(['sort' => 'votes'])->links() !!}
8.3 更多輔助方法
$results->count()
$results->currentPage()
$results->firstItem()
$results->hasMorePages()
$results->lastItem()
$results->lastPage() (使用simplePaginate時(shí)無(wú)效)
$results->nextPageUrl()
$results->perPage()
$results->previousPageUrl()
$results->total() (使用simplePaginate時(shí)無(wú)效)
$results->url($page)
distinct() 允許你強(qiáng)制讓查找返回不重復(fù)的結(jié)果: