Laravel查询构建器提供流畅接口简化数据库操作,支持链式调用、复杂条件、联表、聚合等,基于PDO防止SQL注入;它返回数组或stdClass对象,适用于简单查询或性能敏感场景,而Eloquent ORM则提供模型化、关系映射和事件等高级功能,适合复杂业务逻辑,两者可结合使用,按需选择。

Laravel的查询构建器(Query Builder)提供了一种方便、流畅的接口来创建和运行数据库查询,它抽象了底层SQL的复杂性,让我们能以更PHP友好的方式与数据库交互。简单来说,它就是一套工具,让我们不用手写SQL,就能构建出各种查询语句。而至于查询方法的具体使用,它涵盖了从基础的数据检索到复杂的联表、聚合等一系列操作,旨在简化数据库交互。
深入理解Laravel的查询构建器,我们会发现它就像是数据库操作的瑞士军刀,功能全面且易于上手。它基于
PDO
当我们想用它来查询数据时,通常会从
DB
table
users
use Illuminate\Support\Facades\DB;
// 获取所有用户,返回一个集合(Collection),其中包含stdClass对象
$users = DB::table('users')->get();
// 获取第一个匹配的用户,返回一个stdClass对象或null
$user = DB::table('users')->where('id', 1)->first();
// 查找特定ID的用户,如果不存在则返回null。
// 注意:find方法通常只适用于单主键,且直接传入主键值。
$user = DB::table('users')->find(1);
// 插入一条记录
DB::table('users')->insert([
'name' => 'John Doe',
'email' => 'john@example.com',
'password' => bcrypt('password'), // 密码通常需要加密
'created_at' => now(),
'updated_at' => now(),
]);
// 批量插入多条记录
DB::table('users')->insert([
['name' => 'Jane Doe', 'email' => 'jane@example.com', 'password' => bcrypt('password'), 'created_at' => now(), 'updated_at' => now()],
['name' => 'Peter Pan', 'email' => 'peter@example.com', 'password' => bcrypt('password'), 'created_at' => now(), 'updated_at' => now()],
]);
// 更新记录
DB::table('users')
->where('id', 1)
->update(['email' => 'new_john@example.com', 'updated_at' => now()]);
// 删除记录
DB::table('users')->where('id', 1)->delete();
// 链式调用:结合多个查询条件和排序、限制等
$activeUsers = DB::table('users')
->where('status', 'active')
->orderBy('created_at', 'desc')
->limit(10)
->get();这些方法构成了查询构建器的核心,它们允许我们以一种非常直观的方式来操作数据库,避免了直接编写和拼接SQL字符串时可能出现的错误和安全隐患。
在我刚接触Laravel那会儿,这确实是个让我纠结的问题。简单讲,查询构建器更接近SQL本身,它提供的是一个“表”级别的操作接口,你直接操作的是数据库中的数据行和列。而Eloquent ORM(对象关系映射)则更进一步,它将数据库表映射成PHP对象(模型),让你以面向对象的方式来操作数据。
进销存产品库存管理系统完全基于 WEB 的综合应用解决方案, 真正的 B/S 模式, 使用asp开发, 不需任何安装, 只需一个浏览器, 企业领导, 业务人员, 操作人员可以在不同时间, 地点, 并且可动态, 及时反映企业业务的方方面面. 产品入库,入库查询 库存管理,库存调拨 产品出库,出库查询 统计报表 会员管理 员工管理 工资管理 单位管理 仓库管理 凭证管理 资产管理 流水账管理 产品分类
1689
核心区别在于:
stdClass
我通常会这样选择:
RAW
所以,这不是一个非此即彼的选择,而是根据具体需求来权衡。我甚至经常会在同一个项目中混用两者,取其所长。
查询构建器在处理复杂条件和联表时,确实展现了它的灵活性。它提供了一系列方法来构建这些复杂的SQL语句,而不用我们去担心SQL语法的细节。
复杂条件查询: 除了简单的
where('column', 'value')where
AND
$users = DB::table('users')
->where('status', 'active')
->where('age', '>', 18)
->get();OR
orWhere
$users = DB::table('users')
->where('status', 'active')
->orWhere('is_admin', true)
->get();AND
OR
where
$users = DB::table('users')
->where('status', 'active')
->where(function ($query) {
$query->where('age', '>', 18)
->orWhere('subscribed', true);
})
->get();
// 这会生成类似 WHERE status = 'active' AND (age > 18 OR subscribed = true)whereIn
whereNotIn
whereBetween
whereNotBetween
whereNull
whereNotNull
$users = DB::table('users')->whereIn('id', [1, 2, 3])->get();
$users = DB::table('users')->whereBetween('age', [18, 30])->get();
$users = DB::table('users')->whereNull('email_verified_at')->get();联表操作(Join): 查询构建器也提供了多种
join
join
$usersWithPosts = DB::table('users')
->join('posts', 'users.id', '=', 'posts.user_id')
->select('users.name', 'posts.title')
->get();leftJoin
$usersAndTheirPosts = DB::table('users')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->select('users.name', 'posts.title')
->get();rightJoin
crossJoin
join
join
$usersWithSpecificPosts = DB::table('users')
->join('posts', function ($join) {
$join->on('users.id', '=', 'posts.user_id')
->where('posts.status', 'published');
})
->select('users.name', 'posts.title')
->get();groupBy
having
$postCounts = DB::table('posts')
->select('user_id', DB::raw('count(*) as total_posts'))
->groupBy('user_id以上就是Laravel查询构建器?查询方法如何使用?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号