Laravel框架查詢構(gòu)造器 CURD操作示例
本文實例講述了Laravel框架查詢構(gòu)造器 CURD操作。分享給大家供大家參考,具體如下:
新增
//插入一條數(shù)據(jù)
public function insert(){
$rs = DB::table('student')->insert([
'name' => 'Kit',
'age' => 12
]);
dd($rs); //true
}
//插入一條數(shù)據(jù)并返回自增ID
public function insert(){
$id = DB::table('student')->insertGetId([
'name'=>'Tom',
'age'=>11
]);
dd($id); //1004
}
//插入多條數(shù)據(jù)
public function insert(){
$rs = DB::table('student')->insert([
['name'=>'Ben','age'=>22],
['name'=>'Jean','age'=>23]
]);
dd($rs);//true
}
更新
//更新一條數(shù)據(jù)
public function update(){
$rs = DB::table('student')
->where('id',1003)
->update(['age'=>10]);
dd($rs);//1,返回受影響的行數(shù)
}
//自增更新
public function update(){
//所有年齡加1
$rs = DB::table('student')->increment('age');
dd($rs);//5,返回受影響的行數(shù)
//ID為1001的年齡加3
$rs = DB::table('student')
->where('id',1001)
->increment('age',3);
dd($rs);//1,返回受影響的行數(shù)
}
//自減更新
public function update(){
//所有年齡加1
$rs = DB::table('student')->decrement('age');
dd($rs);//5,返回受影響的行數(shù)
//ID為1001的年齡加3
$rs = DB::table('student')
->where('id',1001)
->decrement('age',3);
dd($rs);//1,返回受影響的行數(shù)
}
//1001年齡加3并且性別改為11
public function update(){
$rs = DB::table('student')
->where('id',1001)
->increment('age',3,['sex'=>11]);
dd($rs);//1,返回受影響的行數(shù)
}
刪除
//刪除ID為1006的數(shù)據(jù)
public function delete(){
$rs = DB::table('student')
->where('id',1006)
->delete();
dd($rs);//1,返回受影響的行數(shù)
}
//刪除ID大于1003的數(shù)據(jù)
public function delete(){
$rs = DB::table('student')
->where('id','>',1003)
->delete();
dd($rs);//2,返回受影響的行數(shù)
}
//清空數(shù)據(jù)表,不返回任何東西
DB::table('student')->truncate();
查詢
- get
- first
- pluck
- select
//查詢所有數(shù)據(jù)
$rs = DB::table('student')->get();
//查詢第一條數(shù)據(jù)
$rs = DB::table('student')->orderBy('id','desc')->first();
//查詢一個name字段
$rs = DB::table('student')->pluck('name');
//查詢name字段并以ID為鍵名
$rs = DB::table('student')->pluck('name','id');
//查詢name,age,sex字段
$rs = DB::table('student')->select('name','age','sex')->get();
聚合函數(shù)
$rs = DB::table('student')->count();
$rs = DB::table('student')->max('age');
$rs = DB::table('student')->min('age');
$rs = DB::table('student')->avg('age');
$rs = DB::table('student')->sum('age');
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Laravel框架的PHP程序設(shè)計有所幫助。
- Laravel5.1 框架數(shù)據(jù)庫查詢構(gòu)建器用法實例詳解
- laravel框架數(shù)據(jù)庫操作、查詢構(gòu)建器、Eloquent ORM操作實例分析
- laravel通用化的CURD的實現(xiàn)
- Laravel框架實現(xiàn)model層的增刪改查(CURD)操作示例
- Laravel框架數(shù)據(jù)庫CURD操作、連貫操作總結(jié)
- laravel5.6 框架操作數(shù)據(jù) Eloquent ORM用法示例
- laravel 操作數(shù)據(jù)庫常用函數(shù)的返回值方法
- laravel框架數(shù)據(jù)庫配置及操作數(shù)據(jù)庫示例
- laravel5.6框架操作數(shù)據(jù)curd寫法(查詢構(gòu)建器)實例分析
相關(guān)文章
PHP實現(xiàn)把MySQL數(shù)據(jù)庫導(dǎo)出為.sql文件實例(仿PHPMyadmin導(dǎo)出功能)
這篇文章主要介紹了PHP實現(xiàn)把MySQL數(shù)據(jù)庫導(dǎo)出為.sql文件實例(仿PHPMyadmin導(dǎo)出功能),需要的朋友可以參考下2014-05-05
PHP中通過trigger_error觸發(fā)PHP錯誤示例
這篇文章主要介紹了PHP中通過trigger_error觸發(fā)PHP錯誤示例,本文介紹了錯誤抑制符@以及通過 trigger_error 觸發(fā) PHP 錯誤示例,需要的朋友可以參考下2015-06-06
關(guān)于PHP 如何用 curl 讀取 HTTP chunked 數(shù)據(jù)
通過本文給大家介紹php用curl讀取http chunked數(shù)據(jù)的方法,本文介紹的非常詳細,具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-02-02
YII中Ueditor富文本編輯器文件和圖片上傳的配置圖文教程
本文主要給大家介紹了YII中Ueditor富文本編輯器文件和圖片上傳的配置圖文教程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03
php使HTML標(biāo)簽自動補全閉合函數(shù)代碼
這個網(wǎng)上找到的自動補全閉合函數(shù)還挺不錯的,它可以根據(jù)你的html內(nèi)容自己補全閉合,確保HTMl代碼正確2012-10-10
將博客園(cnblogs.com)數(shù)據(jù)導(dǎo)入到wordpress的代碼
博客園限制太多,于是決定從博客園(cnblogs)更換自己個人的博客。WORDPRESS口碑還不錯,于是決定用用看。之前發(fā)的數(shù)百篇日志需要導(dǎo)入過來,在網(wǎng)上搜了一會,發(fā)現(xiàn)沒有這個插件,無奈只能自己寫一個2013-01-01
yii2中LinkPager增加總頁數(shù)和總記錄數(shù)的實例
本篇文章主要介紹了php中LinkPager增加總頁數(shù)和總記錄數(shù)的實例,具有一定的參考價值,有興趣的可以了解一下2017-08-08

