laravel框架模型和數(shù)據(jù)庫基礎(chǔ)操作實(shí)例詳解
本文實(shí)例講述了laravel框架模型和數(shù)據(jù)庫基礎(chǔ)操作。分享給大家供大家參考,具體如下:
laravel分為三大數(shù)據(jù)庫操作(DB facade[原始查找],查詢構(gòu)造器[Query Builder],Eloquent ORM):
use Illuminate\Support\Facades\DB;
1.DB facade[原始查找]
$results = DB::select('select * from users where id = :id', ['id' => 1]); DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);
不返回值:
DB::statement('drop table users');
返回自增id:
$id = DB::table('users')->insertGetId( ['email' => 'john@example.com', 'votes' => 0] ); $affected = DB::update('update users set votes = 100 where name = ?', ['John']); $num=DB::delete('delete from vipinfo where vip_ID= ?',[5]);
2.查詢構(gòu)造器[Query Builder]
laravel查詢構(gòu)造器提供了方便流暢的接口,用來建立及執(zhí)行數(shù)據(jù)庫查找語法。使用了pdo參數(shù)綁定,使應(yīng)用程序免于sql注入,因此傳入的參數(shù)不需要額外轉(zhuǎn)義特殊字符?;旧峡梢詽M足所有的數(shù)據(jù)庫操作,而且在所有支持的數(shù)據(jù)庫系統(tǒng)上都可以執(zhí)行。
(1)新增
$bool=DB::table("vipinfo")->insert(['vip_ID'=>6,'vip_name'=>'zls','vip_type'=>"出行",'vip_fenshu'=>800]); echo $bool; //返回bool值 //如果想得到新增的id,則使用insertGetId方法 $id=DB::table("vipinfo")->insertGetId(['vip_ID'=>5,'vip_name'=>'wyp','vip_type'=>"出行",'vip_fenshu'=>800]); echo $id; //插入多條數(shù)據(jù) $bool=DB::table("vipinfo")->insert([ ['vip_ID'=>5,'vip_name'=>'wyp','vip_type'=>"出行",'vip_fenshu'=>800], ['vip_ID'=>6,'vip_name'=>'zls','vip_type'=>"出行",'vip_fenshu'=>800], ]); echo $bool; //返回bool值
(2)修改
$bool=DB::table("vipinfo")->where('vip_ID',6)->update(['vip_fenshu'=>500]); echo $bool; //自增 $bool=DB::table("vipinfo")->where('vip_ID',6)->increment("vip_fenshu");//自增1 $bool=DB::table("vipinfo")->where('vip_ID',6)->increment("vip_fenshu",3);//自增3 echo $bool; //自減 $bool=DB::table("vipinfo")->where('vip_ID',6)->decrement("vip_fenshu");//自1 $bool=DB::table("vipinfo")->where('vip_ID',6)->decrement("vip_fenshu",3);//自增3 echo $bool; //自增時(shí)再修改其他字段 $bool=DB::table("vipinfo")->where('vip_ID',6)->increment("vip_fenshu",3,['vip_name'=>'dbdibi']);//自增3
(3)刪除
$num=DB::table("vipinfo")->where('vip_ID',6)->delete();//刪除1條 $num=DB::table("vipinfo")->where('vip_ID','>',4)->delete();//刪除多條 echo $num; //刪除的行數(shù) $num=DB::table("vipinfo")->truncate();//刪除整表,不能恢復(fù),謹(jǐn)慎使用
(4)查詢
//get()返回多條數(shù)據(jù) $student=DB::table("vipinfo")->get(); var_dump($student); //first()返回1條數(shù)據(jù) $student=DB::table("vipinfo")->first(); //結(jié)果集第一條記錄 $student=DB::table("vipinfo")->orderBy('vip_ID','desc')->first();//按vip_ID倒序排序 var_dump($student); //where()條件查詢 $student=DB::table("vipinfo")->where('vip_ID','>=',2)->get(); //一個(gè)條件 $student=DB::table("vipinfo")->whereRaw('vip_ID> ? and vip_fenshu >= ?',[2,300])->get(); //多個(gè)條件 dd($student); //pluck()指定字段,后面不加get $student=DB::table("vipinfo")->pluck('vip_name'); dd($student); //lists()指定字段,可以指定某個(gè)字段作為下標(biāo) $student=DB::table("vipinfo")->lists('vip_name','vip_ID'); //指定vip_ID為下標(biāo) dd($student); $student=DB::table("vipinfo")->lists('vip_name'); //不指定下標(biāo),默認(rèn)下標(biāo)從0開始 //select()指定某個(gè)字段 $student=DB::table("vipinfo")->select('vip_name','vip_ID')->get(); dd($student); //chunk()每次查n條 $student=DB::table("vipinfo")->chunk(2,function($students){ //每次查2條 var_dump($students); if(.......) return false; //在滿足某個(gè)條件下使用return就不會(huì)再往下查了 });
使用聚合函數(shù)
//count()統(tǒng)計(jì)記錄條數(shù) $nums=DB::table("vipinfo")->count(); echo $nums; //max()某個(gè)字段的最大值,同理min是最小值 $max=DB::table("vipinfo")->max("vip_fenshu"); echo $max; //avg()某個(gè)字段的平均值 $avg=DB::table("vipinfo")->avg("vip_fenshu"); echo $avg; //sum()某個(gè)字段的和 $sum=DB::table("vipinfo")->sum("vip_fenshu"); echo $sum;
3.Eloquent ORM
1.簡(jiǎn)介、模型的建立及查詢數(shù)據(jù)
簡(jiǎn)介:laravel所自帶的Eloquent ORM 是一個(gè)ActiveRecord實(shí)現(xiàn),用于數(shù)據(jù)庫操作。每個(gè)數(shù)據(jù)表都有一個(gè)與之對(duì)應(yīng)的模型,用于數(shù)據(jù)表交互。
建立模型,在app目錄下建立一個(gè)Student模型,即Student.php,不需要帶任何后綴。
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Student extends Model{ //指定表名 protected $table= 'vipinfo'; //指定主鍵 protected $primaryKey= 'vip_ID'; //關(guān)閉laravel自帶更新created_at,updated_at,deleted_at的操作 protected $timestamps= false; //錄入字段名 protected $fillable= ['id','name']; }
在Student控制器里增加一個(gè)test3方法,配置路由
Route::get('test3',['uses'=>'StudentController@test3']);
public function test3(){ // all()方法查詢所有數(shù)據(jù) $studnets=Student::all(); dd($studnets); //find()查詢一條,依據(jù)主鍵查詢。findOrFail()查找不存在的記錄時(shí)會(huì)拋出異常 $student=Student::find(5); //主鍵為5的記錄 var_dump($student['attributes']); //查詢構(gòu)造器的使用,省略了指定表名 $student=Student::get(); var_dump($student); }
2 . 新增數(shù)據(jù)、自定義時(shí)間戳、批量賦值
(1)使用save方法新增
laravel會(huì)默認(rèn)維護(hù)created_at,updated_at 兩個(gè)字段,這兩個(gè)字段都是存儲(chǔ)時(shí)間戳,整型11位的,因此使用時(shí)需要在數(shù)據(jù)庫添加這兩個(gè)字段。如果不需要這個(gè)功能,只需要在模型里加一個(gè)屬性:public $timestamps=false; 以及一個(gè)方法,可以將當(dāng)前時(shí)間戳存到數(shù)據(jù)庫
protected function getDateFormat(){ return time(); }
這樣就不需要那兩個(gè)字段了。
控制器里寫:
$student=new Student(); //設(shè)定數(shù)據(jù) $student->vip_name='xiaoming'; $student->vip_type='出行'; $student->vip_fenshu=900; $bool=$student->save(); //保存 echo $bool;
從數(shù)據(jù)庫里取得某條記錄的時(shí)間戳?xí)r,默認(rèn)取得的是按日期格式化好的時(shí)間戳,如果想取得原本的時(shí)間戳,則在模型里增加asDateTime方法。
protected function asDateTime($val){ return $val; }
(2)使用create方法新增時(shí),需要在模型里增加:
protected $fillable=['vip_name','vip_fenshu','vip_type']; //允許批量賦值的字段
控制器里寫:
Student::create(['vip_name'=>'mmm','vip_fenshu'=>999,'vip_type'=>'出行']);
這樣即可新增成功!
(3)firstOrCreate()以屬性查找記錄,若沒有則新增
$student=Student::firstOrCreate(['vip_name'=>'mmm']); echo $student;
(4)firstOrNew()以屬性查找記錄,若沒有則會(huì)創(chuàng)建新的實(shí)例。若需要保存,則自己調(diào)用save方法()
$student=Student::firstOrNew(['vip_name'=>'mmm']); $student->save(); echo $student;
3. 修改數(shù)據(jù)
使用save方法更新模型
使用update方法更新數(shù)據(jù)(和create相對(duì)應(yīng)的,Eloquent模型類還支持使用update方法更新數(shù)據(jù),同樣要用到批量賦值)
//通過模型更新數(shù)據(jù) $student=Student::find(2); $student->vip_fenshu=10000; $student->save(); //返回bool值 //通過查詢構(gòu)造器更新 $num=Student::where('vip_ID','>',2)->update(['vip_fenshu'=>2000]); echo $num; //返回更新的行數(shù)
4. 刪除數(shù)據(jù)
//(1)通過模型刪除數(shù)據(jù) $student=Student::find(11); $student->delete(); //返回bool值 //(2)通過主鍵刪除 $num=Student::destroy(10); //刪除主鍵為10的一條記錄 echo $num; //返回刪除的行數(shù) $num=Student::destroy(10,5); //刪除多條 或者$num=Student::destroy([10,5]); echo $num; //返回刪除的行數(shù)
視頻資源學(xué)習(xí)參考:http://www.imooc.com/learn/697
更多關(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ù)庫操作技巧匯總》
希望本文所述對(duì)大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
- 在laravel中實(shí)現(xiàn)ORM模型使用第二個(gè)數(shù)據(jù)庫設(shè)置
- 使用laravel的Eloquent模型如何獲取數(shù)據(jù)庫的指定列
- Laravel5.1 框架關(guān)聯(lián)模型之后操作實(shí)例分析
- Laravel5.1 框架模型多態(tài)關(guān)聯(lián)用法實(shí)例分析
- Laravel5.1 框架模型遠(yuǎn)層一對(duì)多關(guān)系實(shí)例分析
- Laravel5.1 框架模型一對(duì)一關(guān)系實(shí)現(xiàn)與使用方法實(shí)例分析
- Laravel5.1 框架模型查詢作用域定義與用法實(shí)例分析
- Laravel5.1 框架模型軟刪除操作實(shí)例分析
- Laravel5.1 框架模型創(chuàng)建與使用方法實(shí)例分析
- Laravel框架視圖和模型操作方法分析
- Laravel 5框架學(xué)習(xí)之模型、控制器、視圖基礎(chǔ)流程
- laravel學(xué)習(xí)教程之關(guān)聯(lián)模型
相關(guān)文章
百度地圖經(jīng)緯度轉(zhuǎn)換到騰訊地圖/Google 對(duì)應(yīng)的經(jīng)緯度
本篇文章主要給大家介紹百度地圖經(jīng)緯度轉(zhuǎn)換到騰訊地圖/Google 對(duì)應(yīng)的經(jīng)緯度.需要的朋友可以參考下2015-08-08CI框架源碼解讀之利用Hook.php文件完成功能擴(kuò)展的方法
這篇文章主要介紹了CI框架源碼解讀之利用Hook.php文件完成功能擴(kuò)展的方法,分析了Hook的原理與擴(kuò)展CI框架的相關(guān)技巧,需要的朋友可以參考下2016-05-05PHP使用fopen與file_get_contents讀取文件實(shí)例分享
這篇文章主要介紹了PHP使用fopen與file_get_contents讀取文件實(shí)例分享的相關(guān)資料,需要的朋友可以參考下2016-03-03yii2.0實(shí)現(xiàn)驗(yàn)證用戶名與郵箱功能
這篇文章主要介紹了yii2.0實(shí)現(xiàn)驗(yàn)證用戶名與郵箱功能的相關(guān)資料,需要的朋友可以參考下2015-12-12