本文实例讲述了Laravel框架Eloquent ORM简介、模型建立及查询数据操作。分享给大家供大家参考,具体如下:
注:以下知识点可能有不全面之处,望见谅
NO.1Eloquent ORM简介
Laravel所自带的Eloquent ORM是一个优美、简洁的ActiveRecord实现,用来实现数据库操作
每个数据表都有与之相对应的“模型(Model)”用于和数据交互
NO.2模型的建立
最基础的模型代码如下:
namespace App; use Illuminate\Database\Eloquent\Model; class Student extends Model { //指定表名 protected $table = 'student'; //指定id protected $primaryKey = 'id'; }
将他创建于app目录下,命名为Student.php
NO.3查询数据
首先在查询之前,我先让你们看一下我的数据库
数据如上,然后查询
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); } }
显示数据库里的所有数据
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); } }
查找指定数据
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); } }
如果他没查到指定的数据,那么他会报错,而find若是没有查到该函数,只会弹出一个null
4.查询构造器的使用
- 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); } }
他会得到一个完整的数据信息,和原本的意义没有区别
- 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.聚合函数的使用
- 1.count函数
代码如下:
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函数
代码如下:
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函数
代码如下:
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函数
代码如下:
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函数
代码如下:
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); } }
更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“Laravel框架Eloquent ORM简介、模型建立及查询数据操作详解”评论...
更新日志
2025年05月07日
2025年05月07日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]