跳至内容

数据库:查询生成器

介绍

Laravel 的数据库查询构建器提供了一个便捷、流畅的界面来创建和运行数据库查询。它可以在你的应用程序中执行大多数数据库操作,并且与 Laravel 支持的所有数据库系统完美兼容。

Laravel 查询构建器使用 PDO 参数绑定来保护您的应用程序免受 SQL 注入攻击。无需清理或净化作为查询绑定传递给查询构建器的字符串。

PDO 不支持绑定列名。因此,您不应允许用户输入指定查询所引用的列名,包括“order by”列。

运行数据库查询

从表中提取所有行

你可以使用外观层table提供的方法DB开始查询。该table方法返回给定表的流畅查询构建器实例,允许你将更多约束链接到查询上,然后最终使用该方法检索查询结果get

1<?php
2 
3namespace App\Http\Controllers;
4 
5use Illuminate\Support\Facades\DB;
6use Illuminate\View\View;
7 
8class UserController extends Controller
9{
10 /**
11 * Show a list of all of the application's users.
12 */
13 public function index(): View
14 {
15 $users = DB::table('users')->get();
16 
17 return view('user.index', ['users' => $users]);
18 }
19}

get方法返回一个Illuminate\Support\Collection包含查询结果的实例,其中每个结果都是一个 PHPstdClass对象的实例。您可以通过将列作为对象的属性来访问每列的值:

1use Illuminate\Support\Facades\DB;
2 
3$users = DB::table('users')->get();
4 
5foreach ($users as $user) {
6 echo $user->name;
7}

Laravel 集合提供了多种极其强大的方法来映射和缩减数据。有关 Laravel 集合的更多信息,请参阅集合文档

从表中检索单行/列

如果你只需要从数据库表中检索一行,可以使用DBFacade 的first方法。该方法将返回一个stdClass对象:

1$user = DB::table('users')->where('name', 'John')->first();
2 
3return $user->email;

如果您想从数据库表中检索单行,但Illuminate\Database\RecordNotFoundException如果未找到匹配的行,则会抛出异常,您可以使用该firstOrFail方法。如果RecordNotFoundException未捕获异常,则会自动向客户端发送 404 HTTP 响应:

1$user = DB::table('users')->where('name', 'John')->firstOrFail();

如果不需要整行,可以使用value方法来从记录中提取单个值。该方法将直接返回该列的值:

1$email = DB::table('users')->where('name', 'John')->value('email');

要按列值检索单行id,请使用该find方法:

1$user = DB::table('users')->find(3);

检索列值列表

如果您想要检索Illuminate\Support\Collection包含单列值的实例,可以使用该pluck方法。在此示例中,我们将检索用户标题的集合:

1use Illuminate\Support\Facades\DB;
2 
3$titles = DB::table('users')->pluck('title');
4 
5foreach ($titles as $title) {
6 echo $title;
7}

您可以通过向该方法提供第二个参数来指定结果集合应该用作其键的列pluck

1$titles = DB::table('users')->pluck('title', 'name');
2 
3foreach ($titles as $name => $title) {
4 echo $title;
5}

分块结果

如果需要处理数千条数据库记录,可以考虑使用Facadechunk提供的方法DB。此方法每次检索一小部分结果,并将每个结果块传入闭包进行处理。例如,让我们users一次检索 100 条记录组成的整张表:

1use Illuminate\Support\Collection;
2use Illuminate\Support\Facades\DB;
3 
4DB::table('users')->orderBy('id')->chunk(100, function (Collection $users) {
5 foreach ($users as $user) {
6 // ...
7 }
8});

false您可以通过从闭包返回来停止处理进一步的块:

1DB::table('users')->orderBy('id')->chunk(100, function (Collection $users) {
2 // Process the records...
3 
4 return false;
5});

如果您在对结果进行分块的同时更新数据库记录,则分块结果可能会发生意外变化。如果您计划在分块时更新检索到的记录,最好始终使用该chunkById方法。此方法将根据记录的主键自动对结果进行分页:

1DB::table('users')->where('active', false)
2 ->chunkById(100, function (Collection $users) {
3 foreach ($users as $user) {
4 DB::table('users')
5 ->where('id', $user->id)
6 ->update(['active' => true]);
7 }
8 });

由于chunkByIdandlazyById方法将它们自己的“where”条件添加到正在执行的查询中,因此您通常应该在闭包内对您自己的条件进行逻辑分组:

1DB::table('users')->where(function ($query) {
2 $query->where('credits', 1)->orWhere('credits', 2);
3})->chunkById(100, function (Collection $users) {
4 foreach ($users as $user) {
5 DB::table('users')
6 ->where('id', $user->id)
7 ->update(['credits' => 3]);
8 }
9});

在分块回调中更新或删除记录时,任何对主键或外键的更改都可能影响分块查询。这可能会导致记录不包含在分块结果中。

延迟流式传输结果

该方法与chunk 方法lazy类似,都是分块执行查询。但是,该方法不会将每个块传入回调函数,而是返回一个LazyCollection,这样您就可以以单个流的形式与结果进行交互:lazy()

1use Illuminate\Support\Facades\DB;
2 
3DB::table('users')->orderBy('id')->lazy()->each(function (object $user) {
4 // ...
5});

再次强调,如果你计划在迭代检索到的记录时更新它们,最好使用lazyByIdlazyByIdDesc方法。这些方法将根据记录的主键自动对结果进行分页:

1DB::table('users')->where('active', false)
2 ->lazyById()->each(function (object $user) {
3 DB::table('users')
4 ->where('id', $user->id)
5 ->update(['active' => true]);
6 });

在迭代记录时更新或删除记录时,任何对主键或外键的更改都可能影响分块查询。这可能会导致记录不包含在结果中。

聚合

查询生成器还提供了多种用于检索聚合值的方法,例如countmaxminavgsum。您可以在构建查询后调用以下任何方法:

1use Illuminate\Support\Facades\DB;
2 
3$users = DB::table('users')->count();
4 
5$price = DB::table('orders')->max('price');

当然,您可以将这些方法与其他子句结合起来,以微调聚合值的计算方式:

1$price = DB::table('orders')
2 ->where('finalized', 1)
3 ->avg('price');

确定记录是否存在

除了使用count方法来确定是否存在符合查询约束的记录之外,您还可以使用existsdoesntExist方法:

1if (DB::table('orders')->where('finalized', 1)->exists()) {
2 // ...
3}
4 
5if (DB::table('orders')->where('finalized', 1)->doesntExist()) {
6 // ...
7}

选择语句

指定 Select 子句

您可能并不总是希望从数据库表中选择所有列。使用该select方法,您可以为查询指定自定义的“select”子句:

1use Illuminate\Support\Facades\DB;
2 
3$users = DB::table('users')
4 ->select('name', 'email as user_email')
5 ->get();

distinct方法允许您强制查询返回不同的结果:

1$users = DB::table('users')->distinct()->get();

如果您已经有一个查询生成器实例,并且希望向其现有的选择子句添加一列,则可以使用该addSelect方法:

1$query = DB::table('users')->select('name');
2 
3$users = $query->addSelect('age')->get();

原始表达式

有时您可能需要在查询中插入任意字符串。要创建原始字符串表达式,可以使用外观raw提供的方法DB

1$users = DB::table('users')
2 ->select(DB::raw('count(*) as user_count, status'))
3 ->where('status', '<>', 1)
4 ->groupBy('status')
5 ->get();

原始语句将作为字符串注入到查询中,因此您应该格外小心,以避免创建 SQL 注入漏洞。

原始方法

除了使用该方法之外DB::raw,您还可以使用以下方法将原始表达式插入查询的各个部分。请记住,Laravel 无法保证任何使用原始表达式的查询都能免受 SQL 注入漏洞的侵害。

selectRaw

selectRaw方法可以代替addSelect(DB::raw(/* ... */))。该方法接受一个可选的绑定数组作为其第二个参数:

1$orders = DB::table('orders')
2 ->selectRaw('price * ? as price_with_tax', [1.0825])
3 ->get();

whereRaw / orWhereRaw

and方法可用于将原始的“where”子句注入到查询中。这些方法接受一个可选的绑定数组作为其第二个参数whereRaworWhereRaw

1$orders = DB::table('orders')
2 ->whereRaw('price > IF(state = "TX", ?, 100)', [200])
3 ->get();

havingRaw / orHavingRaw

方法可用于提供原始字符串作为 "having" 子句的值。这些方法接受一个可选的绑定数组作为其第二个参数havingRaworHavingRaw

1$orders = DB::table('orders')
2 ->select('department', DB::raw('SUM(price) as total_sales'))
3 ->groupBy('department')
4 ->havingRaw('SUM(price) > ?', [2500])
5 ->get();

orderByRaw

orderByRaw方法可用于提供原始字符串作为“order by”子句的值:

1$orders = DB::table('orders')
2 ->orderByRaw('updated_at - created_at DESC')
3 ->get();

groupByRaw

groupByRaw方法可用于提供原始字符串作为group by子句的值:

1$orders = DB::table('orders')
2 ->select('city', 'state')
3 ->groupByRaw('city, state')
4 ->get();

连接

内部连接子句

查询构建器还可用于向查询添加连接子句。要执行基本的“内连接”,您可以join在查询构建器实例上使用该方法。传递给该join方法的第一个参数是您需要连接的表的名称,其余参数指定连接的列约束。您甚至可以在单个查询中连接多个表:

1use Illuminate\Support\Facades\DB;
2 
3$users = DB::table('users')
4 ->join('contacts', 'users.id', '=', 'contacts.user_id')
5 ->join('orders', 'users.id', '=', 'orders.user_id')
6 ->select('users.*', 'contacts.phone', 'orders.price')
7 ->get();

左连接/右连接子句

如果您想执行“左连接”或“右连接”而不是“内连接”,请使用leftJoinrightJoin方法。这些方法具有与方法相同的签名join

1$users = DB::table('users')
2 ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
3 ->get();
4 
5$users = DB::table('users')
6 ->rightJoin('posts', 'users.id', '=', 'posts.user_id')
7 ->get();

交叉连接子句

你可以使用该crossJoin方法执行“交叉连接”。交叉连接会在第一个表和被连接表之间生成笛卡尔积:

1$sizes = DB::table('sizes')
2 ->crossJoin('colors')
3 ->get();

高级连接子句

您还可以指定更高级的连接子句。首先,将一个闭包作为第二个参数传递给该join方法。闭包将接收一个Illuminate\Database\Query\JoinClause实例,该实例允许您在“join”子句上指定约束:

1DB::table('users')
2 ->join('contacts', function (JoinClause $join) {
3 $join->on('users.id', '=', 'contacts.user_id')->orOn(/* ... */);
4 })
5 ->get();

如果您想在连接中使用“where”子句,可以使用实例提供的whereand方法。这些方法不是比较两列,而是将列与某个值进行比较:orWhereJoinClause

1DB::table('users')
2 ->join('contacts', function (JoinClause $join) {
3 $join->on('users.id', '=', 'contacts.user_id')
4 ->where('contacts.user_id', '>', 5);
5 })
6 ->get();

子查询连接

您可以使用joinSubleftJoinSubrightJoinSub方法将查询连接到子查询。每个方法都接收三个参数:子查询、其表别名以及定义相关列的闭包。在此示例中,我们将检索用户集合,其中每个用户记录还包含created_at用户最近发布的博客文章的时间戳:

1$latestPosts = DB::table('posts')
2 ->select('user_id', DB::raw('MAX(created_at) as last_post_created_at'))
3 ->where('is_published', true)
4 ->groupBy('user_id');
5 
6$users = DB::table('users')
7 ->joinSub($latestPosts, 'latest_posts', function (JoinClause $join) {
8 $join->on('users.id', '=', 'latest_posts.user_id');
9 })->get();

横向连接

横向连接目前受 PostgreSQL、MySQL >= 8.0.14 和 SQL Server 支持。

您可以使用joinLateralandleftJoinLateral方法对子查询执行“横向连接”。每个方法都接收两个参数:子查询及其表别名。连接条件应在给where定子查询的子句中指定。横向连接会针对每一行进行评估,并且可以引用子查询外部的列。

在此示例中,我们将检索用户集合以及用户最近的三篇博客文章。每个用户最多可以在结果集中生成三行:每行对应其最近的一篇博客文章。连接条件由whereColumn子查询中的子句指定,引用当前用户的行:

1$latestPosts = DB::table('posts')
2 ->select('id as post_id', 'title as post_title', 'created_at as post_created_at')
3 ->whereColumn('user_id', 'users.id')
4 ->orderBy('created_at', 'desc')
5 ->limit(3);
6 
7$users = DB::table('users')
8 ->joinLateral($latestPosts, 'latest_posts')
9 ->get();

工会

查询生成器还提供了一种便捷的方法,可以将两个或多个查询“合并”在一起。例如,您可以创建一个初始查询,然后使用该union方法将其与更多查询合并:

1use Illuminate\Support\Facades\DB;
2 
3$first = DB::table('users')
4 ->whereNull('first_name');
5 
6$users = DB::table('users')
7 ->whereNull('last_name')
8 ->union($first)
9 ->get();

除了 该union方法之外,查询生成器还提供了一个unionAll方法。使用该方法组合的查询unionAll不会删除重复的结果。 该unionAll方法具有与 该方法相同的方法签名union

基本 Where 子句

Where 子句

您可以使用查询构建器的where方法向查询中添加“where”子句。该方法最基本的调用where需要三个参数。第一个参数是列的名称。第二个参数是一个运算符,可以是数据库支持的任何运算符。第三个参数是要与列值进行比较的值。

例如,以下查询检索列的值votes等于100且列的值age大于的用户35

1$users = DB::table('users')
2 ->where('votes', '=', 100)
3 ->where('age', '>', 35)
4 ->get();

为了方便起见,如果你想验证某个列是否为=给定值,可以将该值作为第二个参数传递给该where方法。Laravel 会假设你使用以下=运算符:

1$users = DB::table('users')->where('votes', 100)->get();

如前所述,您可以使用数据库系统支持的任何运算符:

1$users = DB::table('users')
2 ->where('votes', '>=', 100)
3 ->get();
4 
5$users = DB::table('users')
6 ->where('votes', '<>', 100)
7 ->get();
8 
9$users = DB::table('users')
10 ->where('name', 'like', 'T%')
11 ->get();

您还可以将条件数组传递给where函数。数组的每个元素都应该是一个包含通常传递给该where方法的三个参数的数组:

1$users = DB::table('users')->where([
2 ['status', '=', '1'],
3 ['subscribed', '<>', '1'],
4])->get();

PDO 不支持绑定列名。因此,您不应允许用户输入指定查询所引用的列名,包括“order by”列。

在字符串与数字的比较中,MySQL 和 MariaDB 会自动将字符串转换为整数。在此过程中,非数字字符串会被转换为0,这可能会导致意外结果。例如,如果您的表中有一secret列的值为aaa,并且您运行User::where('secret', 0),则将返回该行。为避免这种情况,请确保在查询中使用所有值之前,先将其转换为适当的类型。

或 Where 子句

当链接调用查询构建器的where方法时,“where”子句将使用 运算符连接在一起and。但是,您可以使用orWhere运算符将子句连接到查询or。该orWhere方法接受与 运算符相同的参数where

1$users = DB::table('users')
2 ->where('votes', '>', 100)
3 ->orWhere('name', 'John')
4 ->get();

如果您需要将“或”条件分组在括号内,则可以将闭包作为第一个参数传递给该orWhere方法:

1use Illuminate\Database\Query\Builder;
2 
3$users = DB::table('users')
4 ->where('votes', '>', 100)
5 ->orWhere(function (Builder $query) {
6 $query->where('name', 'Abigail')
7 ->where('votes', '>', 50);
8 })
9 ->get();

上面的例子将产生以下 SQL:

1select * from users where votes > 100 or (name = 'Abigail' and votes > 50)

您应该始终对orWhere调用进行分组,以避免在应用全局范围时出现意外行为。

Where Not 子句

and方法可用于否定给whereNotorWhereNot的一组查询约束。例如,以下查询排除清仓商品或价格低于 10 美元的商品:

1$products = DB::table('products')
2 ->whereNot(function (Builder $query) {
3 $query->where('clearance', true)
4 ->orWhere('price', '<', 10);
5 })
6 ->get();

Where Any / All / None 子句

有时你可能需要对多个列应用相同的查询约束。例如,你可能希望检索给定列表中所有列包含给LIKE定值的记录。你可以使用以下whereAny命令实现此目的:

1$users = DB::table('users')
2 ->where('active', true)
3 ->whereAny([
4 'name',
5 'email',
6 'phone',
7 ], 'like', 'Example%')
8 ->get();

上述查询将产生以下 SQL:

1SELECT *
2FROM users
3WHERE active = true AND (
4 name LIKE 'Example%' OR
5 email LIKE 'Example%' OR
6 phone LIKE 'Example%'
7)

类似地,该whereAll方法可用于检索所有给定列都符合给定约束的记录:

1$posts = DB::table('posts')
2 ->where('published', true)
3 ->whereAll([
4 'title',
5 'content',
6 ], 'like', '%Laravel%')
7 ->get();

上述查询将产生以下 SQL:

1SELECT *
2FROM posts
3WHERE published = true AND (
4 title LIKE '%Laravel%' AND
5 content LIKE '%Laravel%'
6)

whereNone方法可用于检索没有给定列与给定约束匹配的记录:

1$posts = DB::table('albums')
2 ->where('published', true)
3 ->whereNone([
4 'title',
5 'lyrics',
6 'tags',
7 ], 'like', '%explicit%')
8 ->get();

上述查询将产生以下 SQL:

1SELECT *
2FROM albums
3WHERE published = true AND NOT (
4 title LIKE '%explicit%' OR
5 lyrics LIKE '%explicit%' OR
6 tags LIKE '%explicit%'
7)

JSON Where 子句

Laravel 还支持在支持 JSON 列类型的数据库中查询 JSON 列类型。目前,支持版本包括 MariaDB 10.3+、MySQL 8.0+、PostgreSQL 12.0+、SQL Server 2017+ 和 SQLite 3.39.0+。要查询 JSON 列,请使用以下->运算符:

1$users = DB::table('users')
2 ->where('preferences->dining->meal', 'salad')
3 ->get();

您可以使用以下方式whereJsonContains查询 JSON 数组:

1$users = DB::table('users')
2 ->whereJsonContains('options->languages', 'en')
3 ->get();

如果您的应用程序使用 MariaDB、MySQL 或 PostgreSQL 数据库,您可以将一个值数组传递给该whereJsonContains方法:

1$users = DB::table('users')
2 ->whereJsonContains('options->languages', ['en', 'de'])
3 ->get();

您可以使用whereJsonLength方法根据长度查询 JSON 数组:

1$users = DB::table('users')
2 ->whereJsonLength('options->languages', 0)
3 ->get();
4 
5$users = DB::table('users')
6 ->whereJsonLength('options->languages', '>', 1)
7 ->get();

附加 Where 子句

whereLike / orWhereLike / whereNotLike / orWhereNotLike

whereLike方法允许您在查询中添加“LIKE”子句以进行模式匹配。这些方法提供了一种与数据库无关的字符串匹配查询方式,并能够切换是否区分大小写。默认情况下,字符串匹配不区分大小写:

1$users = DB::table('users')
2 ->whereLike('name', '%John%')
3 ->get();

您可以通过以下参数启用区分大小写的搜索caseSensitive

1$users = DB::table('users')
2 ->whereLike('name', '%John%', caseSensitive: true)
3 ->get();

orWhereLike方法允许您添加带有 LIKE 条件的“或”子句:

1$users = DB::table('users')
2 ->where('votes', '>', 100)
3 ->orWhereLike('name', '%John%')
4 ->get();

whereNotLike方法允许您向查询中添加“NOT LIKE”子句:

1$users = DB::table('users')
2 ->whereNotLike('name', '%John%')
3 ->get();

类似地,您可以使用orWhereNotLike添加带有 NOT LIKE 条件的“或”子句:

1$users = DB::table('users')
2 ->where('votes', '>', 100)
3 ->orWhereNotLike('name', '%John%')
4 ->get();

whereLikeSQL Server 目前不支持区分大小写的搜索选项

whereIn / whereNotIn / orWhereIn / orWhereNotIn

whereIn方法验证给定列的值是否包含在给定的数组中:

1$users = DB::table('users')
2 ->whereIn('id', [1, 2, 3])
3 ->get();

whereNotIn方法验证给定列的值不包含在给定数组中:

1$users = DB::table('users')
2 ->whereNotIn('id', [1, 2, 3])
3 ->get();

您还可以提供查询对象作为该whereIn方法的第二个参数:

1$activeUsers = DB::table('users')->select('id')->where('is_active', 1);
2 
3$users = DB::table('comments')
4 ->whereIn('user_id', $activeUsers)
5 ->get();

上面的例子将产生以下 SQL:

1select * from comments where user_id in (
2 select id
3 from users
4 where is_active = 1
5)

如果您要向查询添加大量整数绑定,则可以使用whereIntegerInRawwhereIntegerNotInRaw方法来大大减少内存使用量。

whereBetween/orWhereBetween

whereBetween方法验证列的值是否介于两个值之间:

1$users = DB::table('users')
2 ->whereBetween('votes', [1, 100])
3 ->get();

whereNotBetween / 或WhereNotBetween

whereNotBetween方法验证列的值是否位于两个值之外:

1$users = DB::table('users')
2 ->whereNotBetween('votes', [1, 100])
3 ->get();

列间位置 / 列间位置不位置 / 列间或位置 / 列间或位置不位置

whereBetweenColumns方法验证列的值是否位于同一表行中两列的两个值之间:

1$patients = DB::table('patients')
2 ->whereBetweenColumns('weight', ['minimum_allowed_weight', 'maximum_allowed_weight'])
3 ->get();

whereNotBetweenColumns方法验证列的值是否位于同一表行中两列的两个值之外:

1$patients = DB::table('patients')
2 ->whereNotBetweenColumns('weight', ['minimum_allowed_weight', 'maximum_allowed_weight'])
3 ->get();

whereNull / whereNotNull / orWhereNull / orWhereNotNull

whereNull方法验证给定列的值是否为NULL

1$users = DB::table('users')
2 ->whereNull('updated_at')
3 ->get();

whereNotNull方法验证列的值不是NULL

1$users = DB::table('users')
2 ->whereNotNull('updated_at')
3 ->get();

whereDate/whereMonth/whereDay/whereYear/whereTime

whereDate方法可用于将列的值与日期进行比较:

1$users = DB::table('users')
2 ->whereDate('created_at', '2016-12-31')
3 ->get();

whereMonth方法可用于将列的值与特定月份进行比较:

1$users = DB::table('users')
2 ->whereMonth('created_at', '12')
3 ->get();

whereDay方法可用于将列的值与月份中的特定日期进行比较:

1$users = DB::table('users')
2 ->whereDay('created_at', '31')
3 ->get();

whereYear方法可用于将列的值与特定年份进行比较:

1$users = DB::table('users')
2 ->whereYear('created_at', '2016')
3 ->get();

whereTime方法可用于将列的值与特定时间进行比较:

1$users = DB::table('users')
2 ->whereTime('created_at', '=', '11:20:45')
3 ->get();

过去在哪里/未来在哪里/今天在哪里/今天之前在哪里/今天之后在哪里

wherePast方法whereFuture可用于确定列的值是过去还是将来:

1$invoices = DB::table('invoices')
2 ->wherePast('due_at')
3 ->get();
4 
5$invoices = DB::table('invoices')
6 ->whereFuture('due_at')
7 ->get();

whereNowOrPast方法whereNowOrFuture可用于确定列的值是过去还是将来,包括当前日期和时间:

1$invoices = DB::table('invoices')
2 ->whereNowOrPast('due_at')
3 ->get();
4 
5$invoices = DB::table('invoices')
6 ->whereNowOrFuture('due_at')
7 ->get();

whereTodaywhereBeforeToday方法whereAfterToday可分别用于确定列的值是今天、今天之前还是今天之后:

1$invoices = DB::table('invoices')
2 ->whereToday('due_at')
3 ->get();
4 
5$invoices = DB::table('invoices')
6 ->whereBeforeToday('due_at')
7 ->get();
8 
9$invoices = DB::table('invoices')
10 ->whereAfterToday('due_at')
11 ->get();

类似地,whereTodayOrBeforewhereTodayOrAfter方法可用于确定列的值是在今天之前还是在今天之后(包括今天的日期):

1$invoices = DB::table('invoices')
2 ->whereTodayOrBefore('due_at')
3 ->get();
4 
5$invoices = DB::table('invoices')
6 ->whereTodayOrAfter('due_at')
7 ->get();

whereColumn/orWhereColumn

whereColumn方法可用于验证两列是否相等:

1$users = DB::table('users')
2 ->whereColumn('first_name', 'last_name')
3 ->get();

您还可以将比较运算符传递给该whereColumn方法:

1$users = DB::table('users')
2 ->whereColumn('updated_at', '>', 'created_at')
3 ->get();

您还可以将列比较数组传递给该whereColumn方法。这些条件将使用and运算符进行连接:

1$users = DB::table('users')
2 ->whereColumn([
3 ['first_name', '=', 'last_name'],
4 ['updated_at', '>', 'created_at'],
5 ])->get();

逻辑分组

有时,您可能需要将多个 where 子句组合在括号内,以实现查询所需的逻辑分组。实际上,通常应该始终将orWhere方法调用组合在括号内,以避免出现意外的查询行为。为此,您可以向where方法传递一个闭包:

1$users = DB::table('users')
2 ->where('name', '=', 'John')
3 ->where(function (Builder $query) {
4 $query->where('votes', '>', 100)
5 ->orWhere('title', '=', 'Admin');
6 })
7 ->get();

如您所见,将闭包传递给where方法会指示查询构建器开始一个约束组。闭包将接收一个查询构建器实例,您可以使用该实例设置应包含在括号组中的约束。上面的示例将生成以下 SQL:

1select * from users where name = 'John' and (votes > 100 or title = 'Admin')

您应该始终对orWhere调用进行分组,以避免在应用全局范围时出现意外行为。

高级 Where 子句

Where Exists 子句

whereExists方法允许你编写“where exist”SQL 子句。该whereExists方法接受一个闭包,该闭包接收一个查询生成器实例,允许你定义应该放在“exists”子句中的查询:

1$users = DB::table('users')
2 ->whereExists(function (Builder $query) {
3 $query->select(DB::raw(1))
4 ->from('orders')
5 ->whereColumn('orders.user_id', 'users.id');
6 })
7 ->get();

或者,您可以向方法提供查询对象whereExists而不是闭包:

1$orders = DB::table('orders')
2 ->select(DB::raw(1))
3 ->whereColumn('orders.user_id', 'users.id');
4 
5$users = DB::table('users')
6 ->whereExists($orders)
7 ->get();

上述两个示例都会产生以下 SQL:

1select * from users
2where exists (
3 select 1
4 from orders
5 where orders.user_id = users.id
6)

子查询 Where 子句

有时您可能需要构造一个“where”子句,将子查询的结果与给定值进行比较。您可以通过向where方法传递一个闭包和一个值来实现。例如,以下查询将检索所有最近拥有给定类型“会员资格”的用户;

1use App\Models\User;
2use Illuminate\Database\Query\Builder;
3 
4$users = User::where(function (Builder $query) {
5 $query->select('type')
6 ->from('membership')
7 ->whereColumn('membership.user_id', 'users.id')
8 ->orderByDesc('membership.start_date')
9 ->limit(1);
10}, 'Pro')->get();

或者,您可能需要构建一个“where”子句,将特定列与子查询的结果进行比较。您可以通过将列、运算符和闭包传递给where方法来实现。例如,以下查询将检索所有收入金额低于平均值的记录;

1use App\Models\Income;
2use Illuminate\Database\Query\Builder;
3 
4$incomes = Income::where('amount', '<', function (Builder $query) {
5 $query->selectRaw('avg(i.amount)')->from('incomes as i');
6})->get();

全文 Where 子句

MariaDB、MySQL 和 PostgreSQL 目前支持全文 where 子句。

whereFullText方法orWhereFullText可用于向具有全文索引的列的查询中添加全文“where”子句。这些方法将被 Laravel 转换为适合底层数据库系统的 SQL。例如,MATCH AGAINST对于使用 MariaDB 或 MySQL 的应用程序,将生成以下子句:

1$users = DB::table('users')
2 ->whereFullText('bio', 'web developer')
3 ->get();

排序、分组、限制和偏移

订购

方法orderBy

orderBy方法允许您按给定列对查询结果进行排序。该orderBy方法接受的第一个参数应该是您希望排序的列,而第二个参数决定排序的方向,可以是ascdesc

1$users = DB::table('users')
2 ->orderBy('name', 'desc')
3 ->get();

要按多列排序,您可以orderBy根据需要调用多次:

1$users = DB::table('users')
2 ->orderBy('name', 'desc')
3 ->orderBy('email', 'asc')
4 ->get();

方法latestoldest

方法允许您轻松地按日期对结果进行排序。默认情况下,结果将按表的列排序latest或者,您可以传递要排序的列名:oldestcreated_at

1$user = DB::table('users')
2 ->latest()
3 ->first();

随机排序

inRandomOrder方法可用于对查询结果进行随机排序。例如,你可以使用此方法来获取随机用户:

1$randomUser = DB::table('users')
2 ->inRandomOrder()
3 ->first();

删除现有排序

reorder方法删除之前应用于查询的所有“order by”子句:

1$query = DB::table('users')->orderBy('name');
2 
3$unorderedUsers = $query->reorder()->get();

您可以在调用该方法时传递列和方向reorder,以删除所有现有的“order by”子句并对查询应用全新的顺序:

1$query = DB::table('users')->orderBy('name');
2 
3$usersOrderedByEmail = $query->reorder('email', 'desc')->get();

分组

方法groupByhaving

正如你所料,groupByhaving方法可用于对查询结果进行分组。该having方法的签名与 类似where

1$users = DB::table('users')
2 ->groupBy('account_id')
3 ->having('account_id', '>', 100)
4 ->get();

您可以使用该havingBetween方法过滤给定范围内的结果:

1$report = DB::table('orders')
2 ->selectRaw('count(id) as number_of_orders, customer_id')
3 ->groupBy('customer_id')
4 ->havingBetween('number_of_orders', [5, 15])
5 ->get();

您可以将多个参数传递给该groupBy方法以按多列进行分组:

1$users = DB::table('users')
2 ->groupBy('first_name', 'status')
3 ->having('account_id', '>', 100)
4 ->get();

要构建更高级的having语句,请参阅havingRaw方法。

限制和偏移

方法skiptake

您可以使用skiptake方法来限制查询返回的结果数,或者跳过查询中给定数量的结果:

1$users = DB::table('users')->skip(10)->take(5)->get();

或者,您可以使用limitoffset方法。这些方法在功能上分别等同于takeskip方法:

1$users = DB::table('users')
2 ->offset(10)
3 ->limit(5)
4 ->get();

条件从句

有时,您可能希望根据其他条件将某些查询子句应用于查询。例如,您可能希望仅当where传入的 HTTP 请求中存在给定的输入值时才应用语句。您可以使用该when方法实现此目的:

1$role = $request->input('role');
2 
3$users = DB::table('users')
4 ->when($role, function (Builder $query, string $role) {
5 $query->where('role_id', $role);
6 })
7 ->get();

when仅当第一个参数为 时,该方法才会执行给定的闭包true。如果第一个参数为false,则不会执行闭包。因此,在上面的示例中,仅当传入请求中存在when该字段且其值为 时,才会调用传递给该方法的闭包roletrue

你可以将另一个闭包作为第三个参数传递给该when方法。只有当第一个参数的计算结果为 时,此闭包才会执行false。为了说明如何使用此功能,我们将使用它来配置查询的默认顺序:

1$sortByVotes = $request->boolean('sort_by_votes');
2 
3$users = DB::table('users')
4 ->when($sortByVotes, function (Builder $query, bool $sortByVotes) {
5 $query->orderBy('votes');
6 }, function (Builder $query) {
7 $query->orderBy('name');
8 })
9 ->get();

插入语句

查询生成器还提供了一个insert方法,可用于将记录插入数据库表。该insert方法接受一个包含列名和值的数组:

1DB::table('users')->insert([
2 'email' => 'kayla@example.com',
3 'votes' => 0
4]);

你可以通过传递一个数组来一次插入多条记录。每个数组代表一条应插入到表中的记录:

1DB::table('users')->insert([
2 ['email' => 'picard@example.com', 'votes' => 0],
3 ['email' => 'janeway@example.com', 'votes' => 0],
4]);

insertOrIgnore方法将在将记录插入数据库时​​忽略错误。使用此方法时,请注意,重复记录错误将被忽略,并且根据数据库引擎的不同,其他类型的错误也可能会被忽略。例如,insertOrIgnore绕过 MySQL 的严格模式

1DB::table('users')->insertOrIgnore([
2 ['id' => 1, 'email' => 'sisko@example.com'],
3 ['id' => 2, 'email' => 'archer@example.com'],
4]);

insertUsing方法将向表中插入新记录,同时使用子查询确定应插入的数据:

1DB::table('pruned_users')->insertUsing([
2 'id', 'name', 'email', 'email_verified_at'
3], DB::table('users')->select(
4 'id', 'name', 'email', 'email_verified_at'
5)->where('updated_at', '<=', now()->subMonth()));

自动递增 ID

如果表具有自动递增 ID,请使用该insertGetId方法插入记录,然后检索 ID:

1$id = DB::table('users')->insertGetId(
2 ['email' => 'john@example.com', 'votes' => 0]
3);

使用 PostgreSQL 时,该insertGetId方法要求自动递增列名为id。如果您想从其他“序列”中检索 ID,可以将列名作为第二个参数传递给该insertGetId方法。

更新插入

upsert方法将插入不存在的记录,并使用您指定的新值更新已存在的记录。该方法的第一个参数包含要插入或更新的值,第二个参数列出关联表中唯一标识记录的列。该方法的第三个也是最后一个参数是一个列数组,如果数据库中已存在匹配的记录,则应更新这些列:

1DB::table('flights')->upsert(
2 [
3 ['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],
4 ['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]
5 ],
6 ['departure', 'destination'],
7 ['price']
8);

departure在上面的例子中,Laravel 将尝试插入两条记录。如果已经存在具有相同和列值的记录destination,Laravel 将更新该记录的price列。

除 SQL Server 外,所有数据库都要求该方法第二个参数中的列upsert具有“主”或“唯一”索引。此外,MariaDB 和 MySQL 数据库驱动程序会忽略该upsert方法的第二个参数,并始终使用表的“主”和“唯一”索引来检测现有记录。

更新语句

除了将记录插入数据库之外,查询生成器还可以使用update方法来更新现有记录。该update方法与 类似insert,接受一个列和值对的数组,用于指示要更新的列。该update方法返回受影响的行数。您可以update使用where子句来约束查询:

1$affected = DB::table('users')
2 ->where('id', 1)
3 ->update(['votes' => 1]);

更新或插入

有时,您可能想要更新数据库中的现有记录,或者在不存在匹配记录的情况下创建一条记录。在这种情况下,updateOrInsert可以使用该方法。该updateOrInsert方法接受两个参数:一个用于查找记录的条件数组,以及一个指示要更新的列的列值对数组。

updateOrInsert方法将尝试使用第一个参数的列和值对来查找匹配的数据库记录。如果记录存在,则使用第二个参数中的值进行更新。如果找不到记录,则将插入一条新记录,其中包含两个参数的合并属性:

1DB::table('users')
2 ->updateOrInsert(
3 ['email' => 'john@example.com', 'name' => 'John'],
4 ['votes' => '2']
5 );

您可以为该updateOrInsert方法提供一个闭包,以根据匹配记录的存在来定制更新或插入数据库的属性:

1DB::table('users')->updateOrInsert(
2 ['user_id' => $user_id],
3 fn ($exists) => $exists ? [
4 'name' => $data['name'],
5 'email' => $data['email'],
6 ] : [
7 'name' => $data['name'],
8 'email' => $data['email'],
9 'marketable' => true,
10 ],
11);

更新 JSON 列

更新 JSON 列时,应使用->语法来更新 JSON 对象中的相应键。MariaDB 10.3+、MySQL 5.7+ 和 PostgreSQL 9.5+ 支持此操作:

1$affected = DB::table('users')
2 ->where('id', 1)
3 ->update(['options->enabled' => true]);

增加和减少

查询生成器还提供了便捷的方法来增加或减少给定列的值。这两个方法都至少接受一个参数:要修改的列。还可以提供第二个参数来指定该列应增加或减少的量:

1DB::table('users')->increment('votes');
2 
3DB::table('users')->increment('votes', 5);
4 
5DB::table('users')->decrement('votes');
6 
7DB::table('users')->decrement('votes', 5);

如果需要,您还可以指定在递增或递减操作期间要更新的其他列:

1DB::table('users')->increment('votes', 1, ['name' => 'John']);

此外,您可以使用incrementEachdecrementEach方法一次增加或减少多个列:

1DB::table('users')->incrementEach([
2 'votes' => 5,
3 'balance' => 100,
4]);

删除语句

查询构建器的delete方法可用于从表中删除记录。该delete方法返回受影响的行数。您可以delete在调用该方法之前添加“where”子句来限制语句delete

1$deleted = DB::table('users')->delete();
2 
3$deleted = DB::table('users')->where('votes', '>', 100)->delete();

悲观锁

查询生成器还包含一些函数,可帮助您在执行select语句时实现“悲观锁定”。要使用“共享锁”执行语句,您可以调用该sharedLock方法。共享锁会阻止所选行在事务提交之前被修改:

1DB::table('users')
2 ->where('votes', '>', 100)
3 ->sharedLock()
4 ->get();

或者,您可以使用以下lockForUpdate方法。“for update”锁可防止所选记录被修改或被另一个共享锁选中:

1DB::table('users')
2 ->where('votes', '>', 100)
3 ->lockForUpdate()
4 ->get();

虽然并非强制,但建议在事务中使用悲观锁。这可以确保检索到的数据在整个操作完成之前在数据库中保持不变。如果发生故障,事务将回滚所有更改并自动释放锁:

1DB::transaction(function () {
2 $sender = DB::table('users')
3 ->lockForUpdate()
4 ->find(1);
5 
6 $receiver = DB::table('users')
7 ->lockForUpdate()
8 ->find(2);
9 
10 if ($sender->balance < 100) {
11 throw new RuntimeException('Balance too low.');
12 }
13 
14 DB::table('users')
15 ->where('id', $sender->id)
16 ->update([
17 'balance' => $sender->balance - 100
18 ]);
19 
20 DB::table('users')
21 ->where('id', $receiver->id)
22 ->update([
23 'balance' => $receiver->balance + 100
24 ]);
25});

可重用查询组件

如果整个应用程序中有重复的查询逻辑,可以使用查询构建器的tappipe方法将其提取到可重用的对象中。假设您的应用程序中有以下两个不同的查询:

1use Illuminate\Database\Query\Builder;
2use Illuminate\Support\Facades\DB;
3 
4$destination = $request->query('destination');
5 
6DB::table('flights')
7 ->when($destination, function (Builder $query, string $destination) {
8 $query->where('destination', $destination);
9 })
10 ->orderByDesc('price')
11 ->get();
12 
13// ...
14 
15$destination = $request->query('destination');
16 
17DB::table('flights')
18 ->when($destination, function (Builder $query, string $destination) {
19 $query->where('destination', $destination);
20 })
21 ->where('user', $request->user()->id)
22 ->orderBy('destination')
23 ->get();

您可能希望将查询之间常见的目标过滤提取到可重用的对象中:

1<?php
2 
3namespace App\Scopes;
4 
5use Illuminate\Database\Query\Builder;
6 
7class DestinationFilter
8{
9 public function __construct(
10 private ?string $destination,
11 ) {
12 //
13 }
14 
15 public function __invoke(Builder $query): void
16 {
17 $query->when($this->destination, function (Builder $query) {
18 $query->where('destination', $this->destination);
19 });
20 }
21}

然后,您可以使用查询构建器的tap方法将对象的逻辑应用于查询:

1use App\Scopes\DestinationFilter;
2use Illuminate\Database\Query\Builder;
3use Illuminate\Support\Facades\DB;
4 
5DB::table('flights')
6 ->when($destination, function (Builder $query, string $destination) {
7 $query->where('destination', $destination);
8 })
9 ->tap(new DestinationFilter($destination))
10 ->orderByDesc('price')
11 ->get();
12 
13// ...
14 
15DB::table('flights')
16 ->when($destination, function (Builder $query, string $destination) {
17 $query->where('destination', $destination);
18 })
19 ->tap(new DestinationFilter($destination))
20 ->where('user', $request->user()->id)
21 ->orderBy('destination')
22 ->get();

查询Pipeline

tap方法始终返回查询生成器。如果您希望提取执行查询并返回其他值的对象,则可以改用 该pipe方法。

考虑以下查询对象,它包含整个应用程序使用的共享分页DestinationFilter逻辑。与将查询条件应用于查询不同,该Paginate对象执行查询并返回分页器实例:

1<?php
2 
3namespace App\Scopes;
4 
5use Illuminate\Contracts\Pagination\LengthAwarePaginator;
6use Illuminate\Database\Query\Builder;
7 
8class Paginate
9{
10 public function __construct(
11 private string $sortBy = 'timestamp',
12 private string $sortDirection = 'desc',
13 private string $perPage = 25,
14 ) {
15 //
16 }
17 
18 public function __invoke(Builder $query): LengthAwarePaginator
19 {
20 return $query->orderBy($this->sortBy, $this->sortDirection)
21 ->paginate($this->perPage, pageName: 'p');
22 }
23}

使用查询构建器的pipe方法,我们可以利用这个对象来应用我们的共享分页逻辑:

1$flights = DB::table('flights')
2 ->tap(new DestinationFilter($destination))
3 ->pipe(new Paginate);

调试

您可以在构建查询时使用dd和方法来转储当前的查询绑定和 SQL。该方法将显示调试信息,然后停止执行请求。 该方法将显示调试信息,但允许请求继续执行:dumpdddump

1DB::table('users')->where('votes', '>', 100)->dd();
2 
3DB::table('users')->where('votes', '>', 100)->dump();

可以在查询上调用dumpRawSqlddRawSql方法来转储查询的 SQL,并正确替换所有参数绑定:

1DB::table('users')->where('votes', '>', 100)->dumpRawSql();
2 
3DB::table('users')->where('votes', '>', 100)->ddRawSql();