本文实例讲述了laravel框架eloquent orm简介、模型建立及查询数据操作。分享给大家供大家参考,具体如下:
注:以下知识点可能有不全面之处,望见谅
no.1eloquent or上能m简介
laravel所自带的eloquent orm是一个优美、简洁的activerecord实现,用来实现数据库操作
每个数据表都有与之相对应的“模型(model)”用于和数据交互
no.2模型的建立
最基础的模型代码如下:
namespace app;u illuminate\databa\eloquent\model;class student extends model{ //指定表名 protected $table = 'student'; //指定id protected $primarykey = 'id';}
将他创建于app目录下,命名为student.php
no.3查询数据
首先在查活动广告宣传语询之前,我先让你们看一下我的数据库
数据如上,然后查询
1.all方式
代码如下:
namespace app\http\controllers;u app\student;u illuminate\support\facades\db;class studentcontroller extends controller{ public function orm1() { $students = student::all(); dd($students); }}
显示数据库里的所有数据
2.find方式
代码如下:
namespace app\http\controllers;u app\student;u illuminate\support\facades\db;class studentcontroller extends controller{ public function orm1() { $尿道炎怎么治students = student::find(1); dd($students); }}
查找指定数据
3.findorfail方式
代码如下:
namespace app\http\controllers;u app\student;u 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;u app\student;u illuminate\support\facades\db;class studentcontroller extends controller{ public function orm1() { $students = student::get(); dd($students); }}
他会得到一个完整的数据信息,和原本的意义没有区别
2.first方式使用代码如下:
namespace app\http\controllers;u app\student;u 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;u app\student;u 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;u app\student;u 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;u app\student;u illuminate\support\facades\db;class studentcontroller extends controller{ public function orm1() { $student = student::count(); dd($student); }}2.max函数
代码如下:
namespace app\http\controllers;u app\student;u illuminate\support\facades\db;class studentcontroller extends controller{ public function orm1() { $student = student::max('age'); dd($student); }}3.min函数
代码如下:
namespace app\http\controllers;u app\student;u illuminate\support\facades\db;class studentcontroller extends controller{ public fun非主流歌ction orm1() { $student = student::min('age'); dd($student); }}4.avg函数
代码如下:
namespace app\http\controllers;u app\student;u illuminate\support\facades\db;class studentcontroller extends controller{ public function orm1() { $student = student::avg('age'); dd($student); }}5.sum函数
代码如下:
namespace app\http\controllers;u app\student;u illuminate\support\facades\db;class studentcontroller extends controller{ public function orm1() { $student = student::sum('age'); dd($student); }}
本文发布于:2023-04-08 04:57:31,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/b8039ac19ea82f2b3767d69f0159c164.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Laravel框架Eloquent ORM简介、模型建立及查询数据操作详解.doc
本文 PDF 下载地址:Laravel框架Eloquent ORM简介、模型建立及查询数据操作详解.pdf
留言与评论(共有 0 条评论) |