Laravel框架查詢構(gòu)造器常見用法總結(jié)
本文實(shí)例講述了Laravel框架查詢構(gòu)造器常見用法。分享給大家供大家參考,具體如下:
查詢構(gòu)造器也是我們使用laravel框架的一項(xiàng)必備技能,上一篇文章我們講到了如何使用原生增刪改查,這一篇我們就來講查詢構(gòu)造器的增刪改查(以下知識點(diǎn)若有不全面的地方,還請多多諒解)
查詢構(gòu)造器簡介:
Laravel查詢構(gòu)造器提供方便流暢的接口,用來建立及執(zhí)行數(shù)據(jù)庫查找語法
使用PDO參數(shù)綁定,以保護(hù)應(yīng)用程序免于SQL注入因此傳入的參數(shù)不需要額外轉(zhuǎn)移特殊字符
基本可以滿足所有數(shù)據(jù)庫操作,而且在所有支持的數(shù)據(jù)庫系統(tǒng)上都可以執(zhí)行
NO.1查詢構(gòu)造器新增數(shù)據(jù)
1.最基本的新增
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur1() { $bool = DB::table('student')//table指的是一個(gè)數(shù)據(jù)表,而括號里的('student')則指的是一個(gè)名為student的數(shù)據(jù)表 ->insert( ['name' => 'Rarin','age' =>16] ); echo "<pre>"; print_r($bool); echo "</pre>"; } }
ok,然后他會返回一個(gè)數(shù)字“1”,返回?cái)?shù)字“1”則證明他已經(jīng)新建了一條數(shù)據(jù)。
2.獲取一個(gè)新增的數(shù)據(jù)id
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur1() { $bool = DB::table('student') ->insertGetId( ['name'=>'Shen' , 'age'=>'16'] ); echo "<pre>"; print_r($bool); echo "</pre>"; } }
然后他會彈出相對應(yīng)的id值,我的id值是2,所以他會顯示一個(gè)數(shù)字“2”。
3.新增多條數(shù)據(jù)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur1() { $bool = DB::table('student') ->insert([ ['name'=>'Chen','age'=>18], ['name'=>'He','age'=>16] ]); var_dump($bool); } }
為了和上面的區(qū)分一下,我把print_r換成了var_dump,他會輸出成一個(gè)true,證明新增成功了。
NO.2查詢構(gòu)造器修改數(shù)據(jù)
1.修改一條數(shù)據(jù)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur2() { $upt = DB::table('student') ->where('id',3) ->update(['age'=>20]); echo "<pre>"; print_r($upt); echo "</pre>"; } }
那么,他會輸出一個(gè)數(shù)字1,代表的是影響的行數(shù)是一行。
2.自增某條數(shù)據(jù)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur2() { $upt = DB::table('student') ->increment('age'); echo "<pre>"; print_r($upt); echo "</pre>"; } }
結(jié)果,他會輸出為4,因?yàn)槲乙还灿兴男袛?shù)據(jù),所以他影響到了四行數(shù)據(jù),自增了1(在這里要說明一下,沒有給他賦值的時(shí)候他默認(rèn)值為1)
那么我們又要如何給他附上值呢?代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur2() { $upt = DB::table('student') ->increment('age',2);//在這里,我們指定他自增的值為2 echo "<pre>"; print_r($upt); echo "</pre>"; } }
那么,他輸出的值照樣是4,證明影響了四個(gè)行數(shù),然后去查看的時(shí)候,可以發(fā)現(xiàn)每個(gè)數(shù)據(jù)的年齡都大了2歲
3.自減某條數(shù)據(jù)
自減的默認(rèn)值和設(shè)置值的操作步驟與自增一致,唯一不同的就是關(guān)鍵字從increment轉(zhuǎn)換成了decrement。
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur2() { $upt = DB::table('student') ->decrement('age'); echo "<pre>"; print_r($upt); echo "</pre>"; } }
然后他會返回一個(gè)受影響行數(shù)的值,我這里有四個(gè)行數(shù)受到了影響,所以返回4
4.根據(jù)某個(gè)條件進(jìn)行自增(自減同理)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur2() { $upt = DB::table('student') ->where('id',1) ->increment('age'); echo "<pre>"; print_r($upt); echo "</pre>"; } }
因?yàn)槭苡绊懙臄?shù)據(jù)只有id是為1的數(shù)據(jù),所以他會返回?cái)?shù)字1,證明受影響的行數(shù)只有一行,自減同理,不過多介紹
自減的時(shí)候修改其他字段(自增同理)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur2() { $upt = DB::table('student') ->where('id',3) ->decrement('age',3,['name' => 'ChenChai']); echo "<pre>"; print_r($upt); echo "</pre>"; } }
他會返回一個(gè)受影響的行數(shù)值
NO.3查詢構(gòu)造器刪除數(shù)據(jù)
1.刪除某條數(shù)據(jù)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur3() { $dlt = DB::table('student') ->where('id',4)//當(dāng)id為4的時(shí)候 ->delete(); echo "<pre>"; print_r($dlt); echo "</pre>"; } }
他會返回一個(gè)數(shù)字1,表示刪除的數(shù)據(jù)共有1行。
2.根據(jù)某個(gè)值刪除多條數(shù)據(jù)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur3() { $dlt = DB::table('student') ->where('id','>=',2)//當(dāng)id大于等于2的時(shí)候 ->delete(); echo "<pre>"; print_r($dlt); echo "</pre>"; } }
他會輸出相對應(yīng)被刪除幾條數(shù)據(jù)的數(shù)字,我這里刪除了2條數(shù)據(jù),所以他返回一個(gè)2
NO.4查詢構(gòu)造器查詢數(shù)據(jù)
1.get方式獲取所有的數(shù)據(jù)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur4() { $slt = DB::table('student') ->get(); dd($slt); } }
2.first方式
獲取第一條數(shù)據(jù)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur4() { $slt = DB::table('student') ->first(); dd($slt); } }
顯示的結(jié)果如下:
重新排序
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur4() { $slt = DB::table('student') ->orderBy('id','desc') ->first(); dd($slt); } }
結(jié)果如下:
數(shù)據(jù)表如下:
3.where方式
插入一條數(shù)據(jù)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur4() { $slt = DB::table('student') ->where('id','>',1)//當(dāng)id大于1的時(shí)候 ->get(); dd($slt); } }
結(jié)果如下:
插入多條數(shù)據(jù)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur4() { $slt = DB::table('student') ->whereRaw('id > ? and age = ?',[1,19])//當(dāng)id大于1的時(shí)候,并且age等于191的時(shí)候 ->get(); dd($slt); } }
4.pluck方式
他只返回某個(gè)值,以數(shù)組的形式
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur4() { $slt = DB::table('student') ->pluck('name'); dd($slt); } }
結(jié)果如下:
5.lists方式
作用和pluck方式差不多,但唯一不同的就是,他可以指定返回的下標(biāo)是多少
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur4() { $slt = DB::table('student') ->lists('name','id');//指定返回的下標(biāo)是和name相對應(yīng)的id dd($slt); } }
6.select方式
你可以用它指定輸出相對應(yīng)的值,而不會像get方式一樣把所有無關(guān)緊要的值也輸出出來
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur4() { $slt = DB::table('student') ->select('name','id','age') ->get(); dd($slt); } }
結(jié)果不做多介紹,只要簡單的理解為指定查找即可
7.chunk方式
這個(gè)一般是用在數(shù)據(jù)過多的時(shí)候,為了流暢,設(shè)定每次輸出多少條數(shù)據(jù)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur4() { DB::table('student') ->chunk(1,function($slt){ echo "<pre>"; print_r($slt); echo "</pre>"; }); } }
如果你只需要查詢一次,只需要添加一個(gè)return false即可。
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur4() { DB::table('student') ->chunk(1,function($slt){ echo "<pre>"; print_r($slt); echo "</pre>"; return false; }); } }
但是一般這種情況都是你滿足了某個(gè)條件后就不再執(zhí)行,所以我們往匿名函數(shù)里增加一個(gè)if判斷語句來判斷再return即可
NO.5查詢構(gòu)造器聚合函數(shù)
1.count函數(shù)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur5() { $num = DB::table('student') ->count(); print_r($num); } }
他這個(gè)函數(shù)是輸出數(shù)據(jù)有幾條記錄,我這里數(shù)據(jù)有2條記錄,所以他會輸出一個(gè)2
2.max函數(shù)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur5() { $num = DB::table('student') ->max('age'); print_r($num); } }
返回?cái)?shù)據(jù)里某個(gè)數(shù)據(jù)的最大值
3.min函數(shù)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur5() { $num = DB::table('student') ->min('age'); print_r($num); } }
與max同理
4.avg函數(shù)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur5() { $num = DB::table('student') ->avg('age'); print_r($num); } }
返回年齡的平均值
5.sum函數(shù)
代碼如下:
namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; class SController extends Controller { public function qur5() { $num = DB::table('student') ->sum('age'); print_r($num); } }
返回?cái)?shù)據(jù)里某個(gè)數(shù)據(jù)的總和
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
上一篇:PHP設(shè)計(jì)模式之觀察者模式入門與應(yīng)用案例詳解
欄 目:PHP編程
本文標(biāo)題:Laravel框架查詢構(gòu)造器常見用法總結(jié)
本文地址:http://mengdiqiu.com.cn/a1/PHPbiancheng/11044.html
您可能感興趣的文章
- 01-11thinkphp框架類庫擴(kuò)展操作示例
- 01-11關(guān)于Yii2框架跑腳本時(shí)內(nèi)存泄漏問題的分析與解決
- 01-11php 實(shí)現(xiàn)簡單的登錄功能示例【基于thinkPHP框架】
- 01-11Laravel 微信小程序后端搭建步驟詳解
- 01-11Laravel框架Blade模板簡介及模板繼承用法分析
- 01-11Laravel 微信小程序后端實(shí)現(xiàn)用戶登錄的示例代碼
- 01-11Laravel框架基礎(chǔ)語法與知識點(diǎn)整理【模板變量、輸出、include引入
- 01-11Laravel框架Eloquent ORM刪除數(shù)據(jù)操作示例
- 01-11Laravel框架Eloquent ORM修改數(shù)據(jù)操作示例
- 01-11Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作詳解


閱讀排行
本欄相關(guān)
- 04-02php本站才可以請求數(shù)據(jù) php本地?cái)?shù)據(jù)庫
- 04-02關(guān)于txt數(shù)據(jù)庫php的信息
- 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
- 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
- 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂
- 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
- 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)
- 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計(jì)方
- 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)
- 04-02php如何用導(dǎo)入數(shù)據(jù) php用來導(dǎo)入其他文
隨機(jī)閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文