Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作詳解
本文實例講述了Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作。分享給大家供大家參考,具體如下:
注:以下知識點可能有不全面之處,望見諒
NO.1Eloquent ORM簡介
Laravel所自帶的Eloquent ORM是一個優(yōu)美、簡潔的ActiveRecord實現(xiàn),用來實現(xiàn)數(shù)據(jù)庫操作
每個數(shù)據(jù)表都有與之相對應(yīng)的“模型(Model)”用于和數(shù)據(jù)交互
NO.2模型的建立
最基礎(chǔ)的模型代碼如下:
namespace App; use Illuminate\Database\Eloquent\Model; class Student extends Model { //指定表名 protected $table = 'student'; //指定id protected $primaryKey = 'id'; }
將他創(chuàng)建于app目錄下,命名為Student.php
NO.3查詢數(shù)據(jù)
首先在查詢之前,我先讓你們看一下我的數(shù)據(jù)庫
數(shù)據(jù)如上,然后查詢
1.all方式
代碼如下:
namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function orm1() { $students = Student::all(); dd($students); } }
顯示數(shù)據(jù)庫里的所有數(shù)據(jù)
2.find方式
代碼如下:
namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function orm1() { $students = Student::find(1); dd($students); } }
查找指定數(shù)據(jù)
3.findOrFail方式
代碼如下:
namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function orm1() { $students = Student::findOrFail(1); dd($students); } }
如果他沒查到指定的數(shù)據(jù),那么他會報錯,而find若是沒有查到該函數(shù),只會彈出一個null
4.查詢構(gòu)造器的使用
- 1.get方式使用
namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function orm1() { $students = Student::get(); dd($students); } }
他會得到一個完整的數(shù)據(jù)信息,和原本的意義沒有區(qū)別
- 2.first方式使用
代碼如下:
namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function orm1() { $student = Student::where('id','>',1) ->orderBy('age','desc') ->first(); dd($student); } }
當id大于一的時候,獲取一個最大值的age
- 3.where方式使用
代碼如下:
namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function orm1() { $student = Student::where('id','>',1) ->get(); dd($student); } }
- 4.chunk方式使用
代碼如下:
namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function orm1() { $student = Student::chunck(2,function($student){ var_dump($student); }); } }
5.聚合函數(shù)的使用
- 1.count函數(shù)
代碼如下:
namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function orm1() { $student = Student::count(); dd($student); } }
- 2.max函數(shù)
代碼如下:
namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function orm1() { $student = Student::max('age'); dd($student); } }
- 3.min函數(shù)
代碼如下:
namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function orm1() { $student = Student::min('age'); dd($student); } }
- 4.avg函數(shù)
代碼如下:
namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function orm1() { $student = Student::avg('age'); dd($student); } }
- 5.sum函數(shù)
代碼如下:
namespace App\Http\Controllers; use App\Student; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function orm1() { $student = Student::sum('age'); dd($student); } }
更多關(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è)計有所幫助。
上一篇:詳解no input file specified 三種解決方法
欄 目:PHP編程
本文標題:Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作詳解
本文地址:http://mengdiqiu.com.cn/a1/PHPbiancheng/11069.html
您可能感興趣的文章
- 01-11thinkphp框架類庫擴展操作示例
- 01-11關(guān)于Yii2框架跑腳本時內(nèi)存泄漏問題的分析與解決
- 01-11php 實現(xiàn)簡單的登錄功能示例【基于thinkPHP框架】
- 01-11Laravel 微信小程序后端搭建步驟詳解
- 01-11Laravel框架Blade模板簡介及模板繼承用法分析
- 01-11Laravel 微信小程序后端實現(xiàn)用戶登錄的示例代碼
- 01-11Laravel框架基礎(chǔ)語法與知識點整理【模板變量、輸出、include引入
- 01-11Laravel框架Eloquent ORM刪除數(shù)據(jù)操作示例
- 01-11Laravel框架Eloquent ORM修改數(shù)據(jù)操作示例
- 01-11laravel5.1框架下的批量賦值實現(xiàn)方法分析


閱讀排行
本欄相關(guān)
- 04-02php本站才可以請求數(shù)據(jù) php本地數(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è)計方
- 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)
- 04-02php如何用導入數(shù)據(jù) php用來導入其他文
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實例總結(jié)