Laravel框架Eloquent ORM简介、模型建立及查询数据操作详解
Laravel  /  管理员 发布于 7年前   175
本文实例讲述了Laravel框架Eloquent ORM简介、模型建立及查询数据操作。分享给大家供大家参考,具体如下: 注:以下知识点可能有不全面之处,望见谅 NO.1Eloquent ORM简介 Laravel所自带的Eloquent ORM是一个优美、简洁的ActiveRecord实现,用来实现数据库操作 NO.2模型的建立 最基础的模型代码如下: 将他创建于app目录下,命名为Student.php NO.3查询数据 首先在查询之前,我先让你们看一下我的数据库 数据如上,然后查询 1.all方式 代码如下: 显示数据库里的所有数据 2.find方式 代码如下: 查找指定数据 3.findOrFail方式 代码如下: 如果他没查到指定的数据,那么他会报错,而find若是没有查到该函数,只会弹出一个null 4.查询构造器的使用 他会得到一个完整的数据信息,和原本的意义没有区别 代码如下: 当id大于一的时候,获取一个最大值的age 代码如下: 代码如下: 5.聚合函数的使用 代码如下: 代码如下: 代码如下: 代码如下: 代码如下: 更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》 希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。
每个数据表都有与之相对应的“模型(Model)”用于和数据交互namespace App;use Illuminate\Database\Eloquent\Model;class Student extends Model{ //指定表名 protected $table = 'student'; //指定id protected $primaryKey = 'id';}
namespace App\Http\Controllers;use App\Student;use Illuminate\Support\Facades\DB;class StudentController extends Controller{ public function orm1() { $students = Student::all(); dd($students); }}
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); }}
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); }}
namespace App\Http\Controllers;use App\Student;use Illuminate\Support\Facades\DB;class StudentController extends Controller{ public function orm1() { $students = Student::get(); dd($students); }}
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); }}
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); }}
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); }); }}
namespace App\Http\Controllers;use App\Student;use Illuminate\Support\Facades\DB;class StudentController extends Controller{ public function orm1() { $student = Student::count(); dd($student); }}
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); }}
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); }}
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); }}
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); }}
您可能感兴趣的文章:
123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号