Skip to content

数据库:查询构建器

简介

Laravel 的数据库查询构建器提供了方便、流式的接口来创建和运行数据库查询。它可以用于在应用中执行大多数数据库操作,并且与 Laravel 支持的所有数据库系统完美兼容。

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

WARNING

PDO 不支持绑定列名。因此,你绝不应允许用户输入决定查询中引用的列名,包括"order by"列。

运行数据库查询

从表中检索所有行

你可以使用 DB 门面提供的 table 方法来开始查询。table 方法为给定表返回一个流式查询构建器实例,允许你在查询上链式添加更多约束,然后最终使用 get 方法检索查询结果:

php
<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\View\View;

class UserController extends Controller
{
    /**
     * Show a list of all of the application's users.
     */
    public function index(): View
    {
        $users = DB::table('users')->get();

        return view('user.index', ['users' => $users]);
    }
}

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

php
use Illuminate\Support\Facades\DB;

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

foreach ($users as $user) {
    echo $user->name;
}

NOTE

Laravel 集合提供了多种极其强大的方法来映射和归约数据。有关 Laravel 集合的更多信息,请查看集合文档

从表中检索单行/单列

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

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

return $user->email;

如果你想从数据库表中检索单行,但在找不到匹配行时抛出 Illuminate\Database\RecordNotFoundException,可以使用 firstOrFail 方法。如果 RecordNotFoundException 未被捕获,将自动向客户端发送 404 HTTP 响应:

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

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

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

要通过其 id 列值检索单行,请使用 find 方法:

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

检索列值列表

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

php
use Illuminate\Support\Facades\DB;

$titles = DB::table('users')->pluck('title');

foreach ($titles as $title) {
    echo $title;
}

你可以通过向 pluck 方法提供第二个参数来指定结果集合应使用的键列:

php
$titles = DB::table('users')->pluck('title', 'name');

foreach ($titles as $name => $title) {
    echo $title;
}

分块结果

如果你需要处理数千条数据库记录,请考虑使用 DB 门面提供的 chunk 方法。此方法一次检索一小块结果,并将每个块提供给闭包进行处理。例如,让我们以每次 100 条记录的块检索整个 users 表:

php
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

DB::table('users')->orderBy('id')->chunk(100, function (Collection $users) {
    foreach ($users as $user) {
        // ...
    }
});

你可以通过从闭包中返回 false 来阻止处理更多块:

php
DB::table('users')->orderBy('id')->chunk(100, function (Collection $users) {
    // Process the records...

    return false;
});

如果在分块结果时更新数据库记录,分块结果可能会以意外方式改变。如果你计划在分块时更新检索到的记录,最好使用 chunkById 方法。此方法将根据记录的主键自动对结果进行分页:

php
DB::table('users')->where('active', false)
    ->chunkById(100, function (Collection $users) {
        foreach ($users as $user) {
            DB::table('users')
                ->where('id', $user->id)
                ->update(['active' => true]);
        }
    });

由于 chunkByIdlazyById 方法会向正在执行的查询添加自己的"where"条件,因此你通常应在闭包内逻辑分组自己的条件:

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

WARNING

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

惰性流式结果

lazy 方法的工作方式类似于分块方法,即它以块的形式执行查询。但是,lazy() 方法不是将每个块传递给回调,而是返回一个 LazyCollection,让你将结果作为单个流进行交互:

php
use Illuminate\Support\Facades\DB;

DB::table('users')->orderBy('id')->lazy()->each(function (object $user) {
    // ...
});

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

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

WARNING

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

聚合

查询构建器还提供了多种检索聚合值的方法,例如 countmaxminavgsum。你可以在构建查询后调用其中任何一个方法:

php
use Illuminate\Support\Facades\DB;

$users = DB::table('users')->count();

$price = DB::table('orders')->max('price');

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

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

判断记录是否存在

除了使用 count 方法来确定是否存在匹配查询约束的记录,你还可以使用 existsdoesntExist 方法:

php
if (DB::table('orders')->where('finalized', 1)->exists()) {
    // ...
}

if (DB::table('orders')->where('finalized', 1)->doesntExist()) {
    // ...
}

Select 语句

指定 Select 子句

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

php
use Illuminate\Support\Facades\DB;

$users = DB::table('users')
    ->select('name', 'email as user_email')
    ->get();

distinct 方法允许你强制查询返回不重复的结果:

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

如果你已经有一个查询构建器实例,并且希望向其现有的 select 子句添加一列,可以使用 addSelect 方法:

php
$query = DB::table('users')->select('name');

$users = $query->addSelect('age')->get();

原生表达式

有时你可能需要在查询中插入任意字符串。要创建原生字符串表达式,可以使用 DB 门面提供的 raw 方法:

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

WARNING

原生语句将作为字符串注入到查询中,因此你应极其小心以避免创建 SQL 注入漏洞。

原生方法

除了使用 DB::raw 方法,你还可以使用以下方法将原生表达式插入到查询的各个部分。请记住,Laravel 无法保证任何使用原生表达式的查询能免受 SQL 注入漏洞的攻击。

selectRaw

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

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

whereRaw / orWhereRaw

whereRaworWhereRaw 方法可用于向查询中注入原生的"where"子句。这些方法接受一个可选的绑定数组作为其第二个参数:

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

havingRaw / orHavingRaw

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

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

orderByRaw

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

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

groupByRaw

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

php
$orders = DB::table('orders')
    ->select('city', 'state')
    ->groupByRaw('city, state')
    ->get();

Joins(连接)

Inner Join 子句

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

php
use Illuminate\Support\Facades\DB;

$users = DB::table('users')
    ->join('contacts', 'users.id', '=', 'contacts.user_id')
    ->join('orders', 'users.id', '=', 'orders.user_id')
    ->select('users.*', 'contacts.phone', 'orders.price')
    ->get();

Left Join / Right Join 子句

如果你想执行"left join"或"right join"而不是"inner join",请使用 leftJoinrightJoin 方法。这些方法与 join 方法具有相同的签名:

php
$users = DB::table('users')
    ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
    ->get();

$users = DB::table('users')
    ->rightJoin('posts', 'users.id', '=', 'posts.user_id')
    ->get();

Cross Join 子句

你可以使用 crossJoin 方法执行"cross join"。Cross join 会在第一个表和连接的表之间生成笛卡尔积:

php
$sizes = DB::table('sizes')
    ->crossJoin('colors')
    ->get();

高级 Join 子句

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

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

如果你想在 join 上使用"where"子句,可以使用 JoinClause 实例提供的 whereorWhere 方法。这些方法不是比较两列,而是将列与值进行比较:

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

子查询 Joins

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

php
$latestPosts = DB::table('posts')
    ->select('user_id', DB::raw('MAX(created_at) as last_post_created_at'))
    ->where('is_published', true)
    ->groupBy('user_id');

$users = DB::table('users')
    ->joinSub($latestPosts, 'latest_posts', function (JoinClause $join) {
        $join->on('users.id', '=', 'latest_posts.user_id');
    })->get();

Lateral Joins

WARNING

Lateral joins 目前支持 PostgreSQL、MySQL >= 8.0.14 和 SQL Server。

你可以使用 joinLateralleftJoinLateral 方法与子查询执行"lateral join"。每个方法接收两个参数:子查询和它的表别名。连接条件应在给定子查询的 where 子句中指定。Lateral joins 会为每一行进行评估,并且可以引用子查询外部的列。

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

php
$latestPosts = DB::table('posts')
    ->select('id as post_id', 'title as post_title', 'created_at as post_created_at')
    ->whereColumn('user_id', 'users.id')
    ->orderBy('created_at', 'desc')
    ->limit(3);

$users = DB::table('users')
    ->joinLateral($latestPosts, 'latest_posts')
    ->get();

Unions(联合)

查询构建器还提供了一种简便的方法将两个或多个查询"union"在一起。例如,你可以创建一个初始查询,并使用 union 方法将其与更多查询联合:

php
use Illuminate\Support\Facades\DB;

$usersWithoutFirstName = DB::table('users')
    ->whereNull('first_name');

$users = DB::table('users')
    ->whereNull('last_name')
    ->union($usersWithoutFirstName)
    ->get();

除了 union 方法,查询构建器还提供了 unionAll 方法。使用 unionAll 方法组合的查询不会移除重复结果。unionAll 方法与 union 方法具有相同的方法签名。

基础 Where 子句

Where 子句

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

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

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

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

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

你还可以向 where 方法传递一个关联数组来快速查询多个列:

php
$users = DB::table('users')->where([
    'first_name' => 'Jane',
    'last_name' => 'Doe',
])->get();

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

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

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

$users = DB::table('users')
    ->where('name', 'like', 'T%')
    ->get();

你也可以向 where 函数传递一个条件数组。数组的每个元素应为一个包含通常传递给 where 方法的三个参数的数组:

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

WARNING

PDO 不支持绑定列名。因此,你绝不应允许用户输入决定查询中引用的列名,包括"order by"列。

WARNING

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

Or Where 子句

当链式调用查询构建器的 where 方法时,"where"子句将使用 and 运算符连接。但是,你可以使用 orWhere 方法通过 or 运算符将子句连接到查询。orWhere 方法接受与 where 方法相同的参数:

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

如果你需要在括号内分组"or"条件,可以将一个闭包作为第一个参数传递给 orWhere 方法:

php
use Illuminate\Database\Query\Builder;

$users = DB::table('users')
    ->where('votes', '>', 100)
    ->orWhere(function (Builder $query) {
        $query->where('name', 'Abigail')
            ->where('votes', '>', 50);
        })
    ->get();

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

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

WARNING

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

Where Not 子句

whereNotorWhereNot 方法可用于否定给定的查询约束组。例如,以下查询排除正在清仓销售或价格低于十美元的产品:

php
$products = DB::table('products')
    ->whereNot(function (Builder $query) {
        $query->where('clearance', true)
            ->orWhere('price', '<', 10);
        })
    ->get();

Where Any / All / None 子句

有时你可能需要将相同的查询约束应用于多个列。例如,你可能想要检索给定列表中任何列 LIKE 给定值的所有记录。你可以使用 whereAny 方法实现:

php
$users = DB::table('users')
    ->where('active', true)
    ->whereAny([
        'name',
        'email',
        'phone',
    ], 'like', 'Example%')
    ->get();

上面的查询将产生以下 SQL:

sql
SELECT *
FROM users
WHERE active = true AND (
    name LIKE 'Example%' OR
    email LIKE 'Example%' OR
    phone LIKE 'Example%'
)

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

php
$posts = DB::table('posts')
    ->where('published', true)
    ->whereAll([
        'title',
        'content',
    ], 'like', '%Laravel%')
    ->get();

上面的查询将产生以下 SQL:

sql
SELECT *
FROM posts
WHERE published = true AND (
    title LIKE '%Laravel%' AND
    content LIKE '%Laravel%'
)

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

php
$albums = DB::table('albums')
    ->where('published', true)
    ->whereNone([
        'title',
        'lyrics',
        'tags',
    ], 'like', '%explicit%')
    ->get();

上面的查询将产生以下 SQL:

sql
SELECT *
FROM albums
WHERE published = true AND NOT (
    title LIKE '%explicit%' OR
    lyrics LIKE '%explicit%' OR
    tags LIKE '%explicit%'
)

JSON Where 子句

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

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

$users = DB::table('users')
    ->whereIn('preferences->dining->meal', ['pasta', 'salad', 'sandwiches'])
    ->get();

你可以使用 whereJsonContainswhereJsonDoesntContain 方法查询 JSON 数组:

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

$users = DB::table('users')
    ->whereJsonDoesntContain('options->languages', 'en')
    ->get();

如果你的应用使用 MariaDB、MySQL 或 PostgreSQL 数据库,你可以向 whereJsonContainswhereJsonDoesntContain 方法传递一个值数组:

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

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

此外,你可以使用 whereJsonContainsKeywhereJsonDoesntContainKey 方法来检索包含或不包含某个 JSON 键的结果:

php
$users = DB::table('users')
    ->whereJsonContainsKey('preferences->dietary_requirements')
    ->get();

$users = DB::table('users')
    ->whereJsonDoesntContainKey('preferences->dietary_requirements')
    ->get();

最后,你可以使用 whereJsonLength 方法根据长度查询 JSON 数组:

php
$users = DB::table('users')
    ->whereJsonLength('options->languages', 0)
    ->get();

$users = DB::table('users')
    ->whereJsonLength('options->languages', '>', 1)
    ->get();

其他 Where 子句

whereLike / orWhereLike / whereNotLike / orWhereNotLike

whereLike 方法允许你向查询添加"LIKE"子句进行模式匹配。这些方法提供了一种数据库无关的方式来执行字符串匹配查询,并能够切换大小写敏感性。默认情况下,字符串匹配不区分大小写:

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

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

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

orWhereLike 方法允许你添加带有 LIKE 条件的"or"子句:

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

whereNotLike 方法允许你向查询添加"NOT LIKE"子句:

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

同样,你可以使用 orWhereNotLike 添加带有 NOT LIKE 条件的"or"子句:

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

WARNING

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

whereIn / whereNotIn / orWhereIn / orWhereNotIn

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

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

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

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

你也可以提供一个查询对象作为 whereIn 方法的第二个参数:

php
$activeUsers = DB::table('users')->select('id')->where('is_active', 1);

$comments = DB::table('comments')
    ->whereIn('user_id', $activeUsers)
    ->get();

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

sql
select * from comments where user_id in (
    select id
    from users
    where is_active = 1
)

WARNING

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

whereBetween / orWhereBetween

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

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

whereNotBetween / orWhereNotBetween

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

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

whereBetweenColumns / whereNotBetweenColumns / orWhereBetweenColumns / orWhereNotBetweenColumns

whereBetweenColumns 方法验证列的值是否在同一表行的两列值之间:

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

whereNotBetweenColumns 方法验证列的值是否在同一表行的两列值之外:

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

whereValueBetween / whereValueNotBetween / orWhereValueBetween / orWhereValueNotBetween

whereValueBetween 方法验证给定值是否在同一表行中两个相同类型列的值之间:

php
$products = DB::table('products')
    ->whereValueBetween(100, ['min_price', 'max_price'])
    ->get();

whereValueNotBetween 方法验证一个值是否在同一表行的两列值之外:

php
$products = DB::table('products')
    ->whereValueNotBetween(100, ['min_price', 'max_price'])
    ->get();

whereNull / whereNotNull / orWhereNull / orWhereNotNull

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

php
$users = DB::table('users')
    ->whereNull('updated_at')
    ->get();

whereNotNull 方法验证列的值不是 NULL

php
$users = DB::table('users')
    ->whereNotNull('updated_at')
    ->get();

whereNullSafeEquals / orWhereNullSafeEquals

whereNullSafeEqualsorWhereNullSafeEquals 方法可用于将列的值与给定值进行比较,同时将两个 NULL 值视为相等:

php
$lastLoginIp = $request->input('last_login_ip');

$users = DB::table('users')
    ->whereNullSafeEquals('last_login_ip', $lastLoginIp)
    ->get();

whereDate / whereMonth / whereDay / whereYear / whereTime

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

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

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

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

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

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

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

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

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

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

wherePast / whereFuture / whereToday / whereBeforeToday / whereAfterToday

wherePastwhereFuture 方法可用于判断列的值是在过去还是将来:

php
$invoices = DB::table('invoices')
    ->wherePast('due_at')
    ->get();

$invoices = DB::table('invoices')
    ->whereFuture('due_at')
    ->get();

whereNowOrPastwhereNowOrFuture 方法可用于判断列的值是在过去还是将来,包含当前日期和时间:

php
$invoices = DB::table('invoices')
    ->whereNowOrPast('due_at')
    ->get();

$invoices = DB::table('invoices')
    ->whereNowOrFuture('due_at')
    ->get();

whereTodaywhereBeforeTodaywhereAfterToday 方法可用于分别判断列的值是今天、今天之前还是今天之后:

php
$invoices = DB::table('invoices')
    ->whereToday('due_at')
    ->get();

$invoices = DB::table('invoices')
    ->whereBeforeToday('due_at')
    ->get();

$invoices = DB::table('invoices')
    ->whereAfterToday('due_at')
    ->get();

类似地,whereTodayOrBeforewhereTodayOrAfter 方法可用于判断列的值是否在今天之前或今天之后,包含今天:

php
$invoices = DB::table('invoices')
    ->whereTodayOrBefore('due_at')
    ->get();

$invoices = DB::table('invoices')
    ->whereTodayOrAfter('due_at')
    ->get();

whereColumn / orWhereColumn

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

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

你也可以向 whereColumn 方法传递比较运算符:

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

你还可以向 whereColumn 方法传递一个列比较数组。这些条件将使用 and 运算符连接:

php
$users = DB::table('users')
    ->whereColumn([
        ['first_name', '=', 'last_name'],
        ['updated_at', '>', 'created_at'],
    ])->get();

逻辑分组

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

php
$users = DB::table('users')
    ->where('name', '=', 'John')
    ->where(function (Builder $query) {
        $query->where('votes', '>', 100)
            ->orWhere('title', '=', 'Admin');
    })
    ->get();

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

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

WARNING

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

高级 Where 子句

Where Exists 子句

whereExists 方法允许你编写"where exists"SQL 子句。whereExists 方法接受一个闭包,该闭包将接收一个查询构建器实例,允许你定义应放置在"exists"子句中的查询:

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

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

php
$orders = DB::table('orders')
    ->select(DB::raw(1))
    ->whereColumn('orders.user_id', 'users.id');

$users = DB::table('users')
    ->whereExists($orders)
    ->get();

上面的两个示例都将产生以下 SQL:

sql
select * from users
where exists (
    select 1
    from orders
    where orders.user_id = users.id
)

子查询 Where 子句

有时你可能需要构建一个"where"子句,将子查询的结果与给定值进行比较。你可以通过向 where 方法传递一个闭包和一个值来实现。例如,以下查询将检索所有具有给定类型近期"membership"的用户:

php
use App\Models\User;
use Illuminate\Database\Query\Builder;

$users = User::where(function (Builder $query) {
    $query->select('type')
        ->from('membership')
        ->whereColumn('membership.user_id', 'users.id')
        ->orderByDesc('membership.start_date')
        ->limit(1);
}, 'Pro')->get();

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

php
use App\Models\Income;
use Illuminate\Database\Query\Builder;

$incomes = Income::where('amount', '<', function (Builder $query) {
    $query->selectRaw('avg(i.amount)')->from('incomes as i');
})->get();

全文 Where 子句

WARNING

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

whereFullTextorWhereFullText 方法可用于向具有全文索引的列添加全文"where"子句。Laravel 会将这些方法转换为底层数据库系统的相应 SQL。例如,对于使用 MariaDB 或 MySQL 的应用,将生成 MATCH AGAINST 子句:

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

向量相似度子句

NOTE

向量相似度子句目前仅支持使用 pgvector 扩展的 PostgreSQL 连接。有关定义向量列和索引的信息,请参阅迁移文档

whereVectorSimilarTo 方法通过余弦相似度过滤与给定向量相关的结果,并按相关性排序结果。minSimilarity 阈值应为 0.01.0 之间的值,其中 1.0 表示完全相同:

php
$documents = DB::table('documents')
    ->whereVectorSimilarTo('embedding', $queryEmbedding, minSimilarity: 0.4)
    ->limit(10)
    ->get();

当给定纯字符串作为向量参数时,Laravel 将使用 Laravel AI SDK 自动为其生成嵌入:

php
$documents = DB::table('documents')
    ->whereVectorSimilarTo('embedding', 'Best wineries in Napa Valley')
    ->limit(10)
    ->get();

默认情况下,whereVectorSimilarTo 还会按距离对结果进行排序(最相似的在前)。你可以通过传递 false 作为 order 参数来禁用此排序:

php
$documents = DB::table('documents')
    ->whereVectorSimilarTo('embedding', $queryEmbedding, minSimilarity: 0.4, order: false)
    ->orderBy('created_at', 'desc')
    ->limit(10)
    ->get();

如果你需要更多控制,可以独立使用 selectVectorDistancewhereVectorDistanceLessThanorderByVectorDistance 方法:

php
$documents = DB::table('documents')
    ->select('*')
    ->selectVectorDistance('embedding', $queryEmbedding, as: 'distance')
    ->whereVectorDistanceLessThan('embedding', $queryEmbedding, maxDistance: 0.3)
    ->orderByVectorDistance('embedding', $queryEmbedding)
    ->limit(10)
    ->get();

使用 PostgreSQL 时,必须加载 pgvector 扩展后才能创建 vector 列:

php
Schema::ensureVectorExtensionExists();

排序、分组、限制与偏移

排序

orderBy 方法

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

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

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

php
$users = DB::table('users')
    ->orderBy('name', 'desc')
    ->orderBy('email', 'asc')
    ->get();

排序方向是可选的,默认为升序。如果你想按降序排序,可以为 orderBy 方法指定第二个参数,或者直接使用 orderByDesc

php
$users = DB::table('users')
    ->orderByDesc('verified_at')
    ->get();

最后,使用 -> 运算符,可以按 JSON 列中的值进行排序:

php
$corporations = DB::table('corporations')
    ->where('country', 'US')
    ->orderBy('location->state')
    ->get();

latestoldest 方法

latestoldest 方法允许你轻松按日期排序结果。默认情况下,结果将按表的 created_at 列排序。或者,你可以传递希望排序的列名:

php
$user = DB::table('users')
    ->latest()
    ->first();

随机排序

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

php
$randomUser = DB::table('users')
    ->inRandomOrder()
    ->first();

移除现有排序

reorder 方法移除之前应用于查询的所有"order by"子句:

php
$query = DB::table('users')->orderBy('name');

$unorderedUsers = $query->reorder()->get();

你可以在调用 reorder 方法时传递列和方向,以移除所有现有的"order by"子句并为查询应用全新的排序:

php
$query = DB::table('users')->orderBy('name');

$usersOrderedByEmail = $query->reorder('email', 'desc')->get();

为了方便,你可以使用 reorderDesc 方法按降序重新排序查询结果:

php
$query = DB::table('users')->orderBy('name');

$usersOrderedByEmail = $query->reorderDesc('email')->get();

分组

groupByhaving 方法

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

php
$users = DB::table('users')
    ->groupBy('account_id')
    ->having('account_id', '>', 100)
    ->get();

你可以使用 havingBetween 方法在给定范围内过滤结果:

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

你可以向 groupBy 方法传递多个参数来按多个列分组:

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

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

限制与偏移

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

php
$users = DB::table('users')
    ->offset(10)
    ->limit(5)
    ->get();

条件子句

有时你可能希望根据另一个条件将某些查询子句应用于查询。例如,你可能只想在传入 HTTP 请求中存在给定输入值时应用 where 语句。你可以使用 when 方法实现:

php
$role = $request->input('role');

$users = DB::table('users')
    ->when($role, function (Builder $query, string $role) {
        $query->where('role_id', $role);
    })
    ->get();

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

你可以向 when 方法传递另一个闭包作为第三个参数。此闭包仅在第一个参数评估为 false 时执行。为了说明如何使用此功能,我们将用它来配置查询的默认排序:

php
$sortByVotes = $request->boolean('sort_by_votes');

$users = DB::table('users')
    ->when($sortByVotes, function (Builder $query, bool $sortByVotes) {
        $query->orderBy('votes');
    }, function (Builder $query) {
        $query->orderBy('name');
    })
    ->get();

Insert 语句

查询构建器还提供了一个 insert 方法,可用于向数据库表中插入记录。insert 方法接受一个列名和值的数组:

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

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

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

insertOrIgnore 方法将忽略向数据库中插入记录时的错误。使用此方法时,你应知道重复记录错误将被忽略,其他类型的错误也可能根据数据库引擎而被忽略。例如,insertOrIgnore绕过 MySQL 的严格模式

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

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

php
DB::table('pruned_users')->insertUsing([
    'id', 'name', 'email', 'email_verified_at'
], DB::table('users')->select(
    'id', 'name', 'email', 'email_verified_at'
)->where('updated_at', '<=', now()->minus(months: 1)));

自增 ID

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

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

WARNING

使用 PostgreSQL 时,insertGetId 方法期望自增列名为 id。如果你想从不同的"序列"检索 ID,可以将列名作为第二个参数传递给 insertGetId 方法。

Upsert(插入或更新)

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

php
DB::table('flights')->upsert(
    [
        ['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],
        ['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]
    ],
    ['departure', 'destination'],
    ['price']
);

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

WARNING

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

Update 语句

除了向数据库中插入记录,查询构建器还可以使用 update 方法更新现有记录。update 方法与 insert 方法一样,接受一个指示要更新的列的列值对数组。update 方法返回受影响的行数。你可以使用 where 子句约束 update 查询:

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

更新或插入

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

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

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

你可以向 updateOrInsert 方法提供一个闭包,以根据匹配记录的存在情况自定义要更新或插入数据库的属性:

php
DB::table('users')->updateOrInsert(
    ['user_id' => $user_id],
    fn ($exists) => $exists ? [
        'name' => $data['name'],
        'email' => $data['email'],
    ] : [
        'name' => $data['name'],
        'email' => $data['email'],
        'marketable' => true,
    ],
);

更新 JSON 列

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

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

自增与自减

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

php
DB::table('users')->increment('votes');

DB::table('users')->increment('votes', 5);

DB::table('users')->decrement('votes');

DB::table('users')->decrement('votes', 5);

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

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

此外,你可以使用 incrementEachdecrementEach 方法一次递增或递减多个列:

php
DB::table('users')->incrementEach([
    'votes' => 5,
    'balance' => 100,
]);

Delete 语句

查询构建器的 delete 方法可用于从表中删除记录。delete 方法返回受影响的行数。你可以在调用 delete 方法之前通过添加"where"子句来约束 delete 语句:

php
$deleted = DB::table('users')->delete();

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

悲观锁

查询构建器还包含几个函数,可帮助你在执行 select 语句时实现"悲观锁"。要执行带有"共享锁"的语句,可以调用 sharedLock 方法。共享锁可防止选定的行在事务提交之前被修改:

php
DB::table('users')
    ->where('votes', '>', 100)
    ->sharedLock()
    ->get();

或者,你可以使用 lockForUpdate 方法。"for update"锁可防止选定的记录被修改或被另一个共享锁选定:

php
DB::table('users')
    ->where('votes', '>', 100)
    ->lockForUpdate()
    ->get();

虽然不是必须的,但建议将悲观锁包装在事务中。这确保了在数据库中找到的相关数据在整个操作完成之前保持不变。如果发生故障,事务将回滚任何更改并自动释放锁:

php
DB::transaction(function () {
    $sender = DB::table('users')
        ->lockForUpdate()
        ->find(1);

    $receiver = DB::table('users')
        ->lockForUpdate()
        ->find(2);

    if ($sender->balance < 100) {
        throw new RuntimeException('Balance too low.');
    }

    DB::table('users')
        ->where('id', $sender->id)
        ->update([
            'balance' => $sender->balance - 100
        ]);

    DB::table('users')
        ->where('id', $receiver->id)
        ->update([
            'balance' => $receiver->balance + 100
        ]);
});

可复用的查询组件

如果你的应用中存在重复的查询逻辑,可以使用查询构建器的 tappipe 方法将逻辑提取为可复用对象。假设你的应用中有这两个不同的查询:

php
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;

$destination = $request->query('destination');

DB::table('flights')
    ->when($destination, function (Builder $query, string $destination) {
        $query->where('destination', $destination);
    })
    ->orderByDesc('price')
    ->get();

// ...

$destination = $request->query('destination');

DB::table('flights')
    ->when($destination, function (Builder $query, string $destination) {
        $query->where('destination', $destination);
    })
    ->where('user', $request->user()->id)
    ->orderBy('destination')
    ->get();

你可能希望将查询之间共有的目的地过滤逻辑提取为可复用对象:

php
<?php

namespace App\Scopes;

use Illuminate\Database\Query\Builder;

class DestinationFilter
{
    public function __construct(
        private ?string $destination,
    ) {
        //
    }

    public function __invoke(Builder $query): void
    {
        $query->when($this->destination, function (Builder $query) {
            $query->where('destination', $this->destination);
        });
    }
}

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

php
use App\Scopes\DestinationFilter;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\DB;

DB::table('flights')
    ->when($destination, function (Builder $query, string $destination) { // [tl! remove]
        $query->where('destination', $destination); // [tl! remove]
    }) // [tl! remove]
    ->tap(new DestinationFilter($destination)) // [tl! add]
    ->orderByDesc('price')
    ->get();

// ...

DB::table('flights')
    ->when($destination, function (Builder $query, string $destination) { // [tl! remove]
        $query->where('destination', $destination); // [tl! remove]
    }) // [tl! remove]
    ->tap(new DestinationFilter($destination)) // [tl! add]
    ->where('user', $request->user()->id)
    ->orderBy('destination')
    ->get();

查询管道

tap 方法将始终返回查询构建器。如果你想要提取一个执行查询并返回另一个值的对象,可以使用 pipe 方法。

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

php
<?php

namespace App\Scopes;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Query\Builder;

class Paginate
{
    public function __construct(
        private string $sortBy = 'timestamp',
        private string $sortDirection = 'desc',
        private int $perPage = 25,
    ) {
        //
    }

    public function __invoke(Builder $query): LengthAwarePaginator
    {
        return $query->orderBy($this->sortBy, $this->sortDirection)
            ->paginate($this->perPage, pageName: 'p');
    }
}

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

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

调试

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

php
DB::table('users')->where('votes', '>', 100)->dd();

DB::table('users')->where('votes', '>', 100)->dump();

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

php
DB::table('users')->where('votes', '>', 100)->dumpRawSql();

DB::table('users')->where('votes', '>', 100)->ddRawSql();

基于 MIT 协议发布