本文实例讲述了Laravel框架Eloquent ORM简介、模型建立及查询数据操作。分享给大家供大家参考,具体如下:
注:以下知识点可能有不全面之处,望见谅
Laravel所自带的Eloquent ORM是一个优美、简洁的ActiveRecord实现,用来实现数据库操作
每个数据表都有与之相对应的“模型(Model)”用于和数据交互
NO.2模型的建立
最基础的模型代码如下:
?
|
1
2
3
4
5
6
7
8
9
|
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方式
代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
|
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方式
代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
|
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方式
代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
|
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方式使用
?
|
1
2
3
4
5
6
7
8
9
10
11
|
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方式使用
代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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方式使用
代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
|
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方式使用
代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
|
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函数
代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
|
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函数
代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
|
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函数
代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
|
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函数
代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
|
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函数
代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
|
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框架的PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/weixin_44596681/article/details/89083478
相关文章
猜你喜欢
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 33
-
2025-05-29 82
-
2025-05-25 17
-
2025-06-04 53
-
2025-05-29 36
热门评论


