# Database: Query Builder

- [Introduction](#introduction)
- [Running Database Queries](#running-database-queries)
    - [Chunking Results](#chunking-results)
    - [Streaming Results Lazily](#streaming-results-lazily)
    - [Aggregates](#aggregates)
- [Select Statements](#select-statements)
- [Raw Expressions](#raw-expressions)
- [Joins](#joins)
- [Unions](#unions)
- [Basic Where Clauses](#basic-where-clauses)
    - [Where Clauses](#where-clauses)
    - [Or Where Clauses](#or-where-clauses)
    - [Where Not Clauses](#where-not-clauses)
    - [Where Any / All / None Clauses](#where-any-all-none-clauses)
    - [JSON Where Clauses](#json-where-clauses)
    - [Additional Where Clauses](#additional-where-clauses)
    - [Logical Grouping](#logical-grouping)
- [Advanced Where Clauses](#advanced-where-clauses)
    - [Where Exists Clauses](#where-exists-clauses)
    - [Subquery Where Clauses](#subquery-where-clauses)
    - [Full Text Where Clauses](#full-text-where-clauses)
    - [Vector Similarity Clauses](#vector-similarity-clauses)
- [Ordering, Grouping, Limit and Offset](#ordering-grouping-limit-and-offset)
    - [Ordering](#ordering)
    - [Grouping](#grouping)
    - [Limit and Offset](#limit-and-offset)
- [Conditional Clauses](#conditional-clauses)
- [Insert Statements](#insert-statements)
    - [Upserts](#upserts)
- [Update Statements](#update-statements)
    - [Updating JSON Columns](#updating-json-columns)
    - [Increment and Decrement](#increment-and-decrement)
- [Delete Statements](#delete-statements)
- [Pessimistic Locking](#pessimistic-locking)
- [Reusable Query Components](#reusable-query-components)
- [Debugging](#debugging)

<a name="introduction"></a>

## Introduction


Laravel のデータベース クエリビルダは、データベース クエリを作成および実行するための便利で流暢なインターフェイスを提供します。これはアプリケーションでほとんどのデータベース操作を実行するために使用でき、Laravel でサポートされているすべてのデータベース システムと完全に連携します。


Laravel クエリビルダは、PDO パラメーター バインディングを使用して、アプリケーションを SQL インジェクション攻撃から保護します。クエリ バインディングとしてクエリビルダに渡される文字列をクリーンアップまたはサニタイズする必要はありません。

> [!WARNING]
> PDOはカラム名のバインドをサポートしていません。そのため、「order by」句のカラムを含め、クエリで参照するカラム名をユーザー入力によって決定できるようにしてはいけません。

<a name="running-database-queries"></a>

## Running Database Queries

<a name="retrieving-all-rows-from-a-table"></a>

#### Retrieving All Rows From a Table


`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 のコレクションについて詳しくは、[collection documentation](/ja/php/docs/collections/)を参照してください。

<a name="retrieving-a-single-row-column-from-a-table"></a>

#### Retrieving a Single Row / Column From a Table


データベース テーブルから 1 つの行を取得するだけの場合は、`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');
```


`soleValue` メソッドは、クエリに一致する行がちょうど1行の場合に限り、カラムの値を取得します。一致する行が見つからない場合は `Illuminate\Database\RecordsNotFoundException` がスローされます。一致する行が2行以上ある場合は `Illuminate\Database\MultipleRecordsFoundException` がスローされます。

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


`id` 列の値によって単一行を取得するには、`find` メソッドを使用します。

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

<a name="retrieving-a-list-of-column-values"></a>

#### Retrieving a List of Column Values


単一列の値を含む `Illuminate\Support\Collection` インスタンスを取得したい場合は、`pluck` メソッドを使用できます。この例では、ユーザーのタイトルのコレクションを取得します。

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

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

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


`pluck` メソッドに 2 番目の引数を指定することで、結果のコレクションがキーとして使用する列を指定できます。

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

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

<a name="chunking-results"></a>

### Chunking Results


数千のデータベース レコードを操作する必要がある場合は、`DB` ファサードによって提供される `chunk` メソッドの使用を検討してください。このメソッドは、一度に結果の小さなチャンクを取得し、各チャンクを処理のためにクロージャにフィードします。たとえば、`users` テーブル全体を一度に 100 レコードずつ取得してみましょう。

```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]);
        }
    });
```


`chunkById` メソッドと `lazyById` メソッドは、実行されるクエリに独自の「where」条件を追加するため、通常はクロージャ内で独自の条件を [logically group](#logical-grouping) する必要があります。

```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]
> チャンクコールバック内でレコードを更新または削除する場合、主キーや外部キーを変更するとチャンククエリに影響する可能性があります。その結果、チャンク処理の結果に含まれないレコードが発生するおそれがあります。

<a name="streaming-results-lazily"></a>

### Streaming Results Lazily


`lazy` メソッドは、クエリをチャンク単位で実行するという点で、[the chunk method](#chunking-results) と同様に動作します。ただし、各チャンクをコールバックに渡す代わりに、`lazy()` メソッドは [LazyCollection](/ja/php/docs/collections/#lazy-collections) を返します。これにより、結果を単一のストリームとして操作できます。

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

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


繰り返しになりますが、取得したレコードを反復処理しながら更新する場合は、代わりに `lazyById` メソッドまたは `lazyByIdDesc` メソッドを使用することをお勧めします。これらのメソッドは、レコードの主キーに基づいて結果を自動的にページ分割します。

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

> [!WARNING]
> レコードを反復処理しながら更新または削除する場合、主キーや外部キーを変更するとチャンククエリに影響する可能性があります。その結果、レコードが結果に含まれなくなる可能性があります。

<a name="aggregates"></a>

### Aggregates


クエリビルダは、`count`、`max`、`min`、`avg`、`sum` などの集計値を取得するためのさまざまなメソッドも提供します。クエリを作成した後、次のメソッドのいずれかを呼び出すことができます。

```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');
```

<a name="determining-if-records-exist"></a>

#### Determining if Records Exist


`count` メソッドを使用してクエリの制約に一致するレコードが存在するかどうかを確認する代わりに、`exists` メソッドと `doesntExist` メソッドを使用することもできます。

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

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

<a name="select-statements"></a>

## Select Statements

<a name="specifying-a-select-clause"></a>

#### Specifying a Select Clause


データベース テーブルからすべての列を選択する必要がない場合もあります。 `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();
```


クエリビルダ インスタンスがすでにあり、その既存の選択句に列を追加したい場合は、`addSelect` メソッドを使用できます。

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

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

<a name="raw-expressions"></a>

## Raw Expressions


場合によっては、クエリに任意の文字列を挿入する必要があるかもしれません。生の文字列式を作成するには、`DB` ファサードによって提供される `raw` メソッドを使用できます。

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

> [!WARNING]
> Raw ステートメントは文字列としてクエリに挿入されるため、SQL インジェクションの脆弱性を生じさせないよう十分に注意してください。

<a name="raw-methods"></a>

### Raw Methods


`DB::raw` メソッドを使用する代わりに、次のメソッドを使用してクエリのさまざまな部分に生の式を挿入することもできます。 **Laravel では、生の式を使用したクエリが SQL インジェクションの脆弱性から保護されることを保証できないことに注意してください。**

<a name="selectraw"></a>

#### `selectRaw`


`selectRaw` メソッドは、`addSelect(DB::raw(/* ... */))` の代わりに使用できます。このメソッドは、オプションのバインディングの配列を 2 番目の引数として受け入れます。

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

<a name="whereraw-orwhereraw"></a>

#### `whereRaw / orWhereRaw`


`whereRaw` メソッドと `orWhereRaw` メソッドを使用して、生の "where" 句をクエリに挿入できます。これらのメソッドは、オプションのバインディングの配列を 2 番目の引数として受け入れます。

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

<a name="havingraw-orhavingraw"></a>

#### `havingRaw / orHavingRaw`


`havingRaw` メソッドと `orHavingRaw` メソッドを使用して、生の文字列を「having」句の値として提供できます。これらのメソッドは、オプションのバインディングの配列を 2 番目の引数として受け入れます。

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

<a name="orderbyraw"></a>

#### `orderByRaw`


`orderByRaw` メソッドを使用して、生の文字列を「order by」句の値として提供できます。

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

<a name="groupbyraw"></a>

#### `groupByRaw`


`groupByRaw` メソッドを使用して、生の文字列を `group by` 句の値として提供できます。

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

<a name="joins"></a>

## Joins

<a name="inner-join-clause"></a>

#### Inner Join Clause


クエリビルダは、クエリに結合句を追加するために使用することもできます。基本的な「内部結合」を実行するには、クエリビルダ インスタンスで `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();
```

<a name="left-join-right-join-clause"></a>

#### Left Join / Right Join Clause


「内部結合」の代わりに「左結合」または「右結合」を実行したい場合は、`leftJoin` メソッドまたは `rightJoin` メソッドを使用します。これらのメソッドは、`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();
```

<a name="cross-join-clause"></a>

#### Cross Join Clause


`crossJoin` メソッドを使用して「クロス結合」を実行できます。クロス結合では、最初のテーブルと結合されたテーブルの間にデカルト積が生成されます。

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

<a name="advanced-join-clauses"></a>

#### Advanced Join Clauses


より高度な結合句を指定することもできます。まず、2 番目の引数としてクロージャを `join` メソッドに渡します。クロージャは、「join」句に制約を指定できる `Illuminate\Database\Query\JoinClause` インスタンスを受け取ります。

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


結合で「where」句を使用したい場合は、`JoinClause` インスタンスによって提供される `where` メソッドと `orWhere` メソッドを使用できます。これらのメソッドは、2 つの列を比較する代わりに、列を値と比較します。

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

<a name="subquery-joins"></a>

#### Subquery Joins


`joinSub`、`leftJoinSub`、および `rightJoinSub` メソッドを使用して、クエリをサブクエリに結合できます。これらの各メソッドは、サブクエリ、そのテーブル エイリアス、および関連する列を定義するクロージャという 3 つの引数を受け取ります。この例では、ユーザーのコレクションを取得します。各ユーザー レコードには、ユーザーが最後に公開したブログ投稿の `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();
```

<a name="lateral-joins"></a>

#### Lateral Joins

> [!WARNING]
> Lateral join は現在、PostgreSQL、MySQL >= 8.0.14、SQL Server でサポートされています。


`joinLateral` メソッドと `leftJoinLateral` メソッドを使用して、サブクエリとの「横結合」を実行できます。これらの各メソッドは、サブクエリとそのテーブル エイリアスの 2 つの引数を受け取ります。結合条件は、指定されたサブクエリの `where` 句内で指定する必要があります。横方向結合は行ごとに評価され、サブクエリの外部の列を参照できます。


この例では、ユーザーのコレクションとユーザーの最新の 3 つのブログ投稿を取得します。各ユーザーは、結果セット内に最大 3 行 (最新のブログ投稿ごとに 1 行) を生成できます。結合条件は、サブクエリ内の `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();
```

<a name="unions"></a>

## Unions


クエリビルダは、2 つ以上のクエリを「結合」する便利な方法も提供します。たとえば、最初のクエリを作成し、`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` メソッドと同じメソッド シグネチャがあります。

<a name="basic-where-clauses"></a>

## Basic Where Clauses

<a name="where-clauses"></a>

### Where Clauses


クエリビルダの `where` メソッドを使用して、クエリに「where」句を追加できます。 `where` メソッドの最も基本的な呼び出しには 3 つの引数が必要です。最初の引数は列の名前です。 2 番目の引数は演算子で、データベースでサポートされている演算子のいずれかを使用できます。 3 番目の引数は、列の値と比較する値です。


たとえば、次のクエリは、`votes` 列の値が `100` に等しく、`age` 列の値が `35` より大きいユーザーを取得します。

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


便宜上、列が特定の値に対して `=` であることを確認したい場合は、その値を 2 番目の引数として `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` メソッドに渡される 3 つの引数を含む配列である必要があります。

```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)` を実行すると、その行が返されます。これを避けるには、クエリで使用する前に、すべての値を適切な型へ型変換してください。

<a name="or-where-clauses"></a>

### Or Where Clauses


クエリビルダの `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` の呼び出しは必ずグループ化してください。

<a name="where-not-clauses"></a>

### Where Not Clauses


`whereNot` メソッドと `orWhereNot` メソッドは、クエリ制約の特定のグループを無効にするために使用できます。たとえば、次のクエリでは、在庫処分中の製品や価格が 10 未満の製品が除外されます。

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

<a name="where-any-all-none-clauses"></a>

### Where Any / All / None Clauses


場合によっては、同じクエリ制約を複数の列に適用する必要がある場合があります。たとえば、指定されたリスト内の列が指定された値 `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%'
)
```

<a name="json-where-clauses"></a>

### JSON Where Clauses


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();
```


`whereJsonContains` メソッドと `whereJsonDoesntContain` メソッドを使用して、JSON 配列をクエリできます。

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

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


アプリケーションが MariaDB、MySQL、または PostgreSQL データベースを使用している場合は、値の配列を `whereJsonContains` および `whereJsonDoesntContain` メソッドに渡すことができます。

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

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


さらに、`whereJsonContainsKey` メソッドまたは `whereJsonDoesntContainKey` メソッドを使用して、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();
```

<a name="additional-where-clauses"></a>

### Additional Where Clauses


**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]
> SQL Server では、`whereLike` の大文字と小文字を区別する検索オプションは現在サポートされていません。


**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` メソッドの 2 番目の引数としてクエリ オブジェクトを指定することもできます。

```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
)
```

> [!NOTE]
> クエリに大量の整数バインディングを追加する場合は、`whereIntegerInRaw` または `whereIntegerNotInRaw` メソッドを使うと、メモリ使用量を大幅に削減できます。


**whereBetween / orWhereBetween**


`whereBetween` メソッドは、列の値が 2 つの値の間にあることを検証します。

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


**whereNotBetween / orWhereNotBetween**


`whereNotBetween` メソッドは、列の値が次の 2 つの値の範囲外にあるかどうかを検証します。

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


**whereBetweenColumns / whereNotBetweenColumns / orWhereBetweenColumns / orWhereNotBetweenColumns**


`whereBetweenColumns` メソッドは、列の値が、同じテーブル行内の 2 つの列の 2 つの値の間にあることを検証します。

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


`whereNotBetweenColumns` メソッドは、列の値が同じテーブル行内の 2 つの列の 2 つの値の外側にあることを検証します。

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


**whereValueBetween / whereValueNotBetween / orWhereValueBetween / orWhereValueNotBetween**


`whereValueBetween` メソッドは、指定された値が、同じテーブル行内の同じ型の 2 つの列の値の間にあることを検証します。

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


`whereValueNotBetween` メソッドは、値が同じテーブル行の 2 つの列の値の外側にあることを検証します。

```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**


`whereNullSafeEquals` と `orWhereNullSafeEquals` メソッドは、2つの `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**


`wherePast` メソッドと `whereFuture` メソッドは、列の値が過去のものであるか未来のものであるかを判断するために使用できます。

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

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


`whereNowOrPast` メソッドと `whereNowOrFuture` メソッドは、現在の日付と時刻を含め、列の値が過去のものであるか未来のものであるかを判断するために使用できます。

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

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


`whereToday`、`whereBeforeToday`、および `whereAfterToday` メソッドは、それぞれ列の値が今日、今日より前、または今日以降であるかどうかを判断するために使用できます。

```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();
```


同様に、`whereTodayOrBefore` メソッドと `whereTodayOrAfter` メソッドを使用して、列の値が今日より前であるか今日以降であるかを判断できます (今日の日付も含みます)。

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

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


**whereColumn / orWhereColumn**


`whereColumn` メソッドを使用して、2 つの列が等しいことを確認できます。

```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();
```

<a name="logical-grouping"></a>

### Logical Grouping


場合によっては、クエリで目的の論理グループを作成するために、複数の "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` の呼び出しは必ずグループ化してください。

<a name="advanced-where-clauses"></a>

## Advanced Where Clauses

<a name="where-exists-clauses"></a>

### Where Exists Clauses


`whereExists` メソッドを使用すると、「存在する場所」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
)
```

<a name="subquery-where-clauses"></a>

### Subquery Where Clauses


場合によっては、サブクエリの結果を指定された値と比較する「where」句を作成する必要があるかもしれません。これを行うには、クロージャと値を `where` メソッドに渡します。たとえば、次のクエリは、特定のタイプの最近の「メンバーシップ」を持つすべてのユーザーを取得します。

```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();
```

<a name="full-text-where-clauses"></a>

### Full Text Where Clauses

> [!WARNING]
> 現在、全文の where 句は MariaDB、MySQL、PostgreSQL でのみサポートされています。


`whereFullText` メソッドと `orWhereFullText` メソッドを使用すると、[full text indexes](/ja/php/docs/migrations/#available-index-types) が設定されたカラムを対象とするクエリに、フルテキスト検索用の「where」句を追加できます。Laravel は、これらのメソッドを使用して、基盤となるデータベースシステムに適した SQL に変換します。たとえば、MariaDB または MySQL を使用するアプリケーションでは、`MATCH AGAINST` 句が生成されます。

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

<a name="vector-similarity-clauses"></a>

### Vector Similarity Clauses

> [!NOTE]
> ベクトル類似度の条件は、現在、`pgvector` 拡張機能を使用する PostgreSQL 接続と、MariaDB 11.7 以降でサポートされています。ベクトルのカラムとインデックスの定義については、[migration documentation](/ja/php/docs/migrations/#available-column-types)を参照してください。


`whereVectorSimilarTo` メソッドは、指定されたベクトルに対するコサイン類似度によって結果をフィルター処理し、関連性によって結果を順序付けします。 `minSimilarity` しきい値は、`0.0` と `1.0` の間の値である必要があります。ここで、`1.0` は同一です。

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


ベクトル引数にプレーンな文字列を渡すと、Laravel は [Laravel AI SDK](/ja/php/docs/ai-sdk/#embeddings) を使用して、その文字列の埋め込みを自動的に生成します。

```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();
```


より詳細な制御が必要な場合は、`selectVectorDistance`、`whereVectorDistanceLessThan`、および `orderByVectorDistance` メソッドを個別に使用できます。

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


PostgreSQL を利用する場合、`vector` 列を作成する前に、`pgvector` 拡張機能をロードする必要があります。

```php
Schema::ensureVectorExtensionExists();
```

<a name="ordering-grouping-limit-and-offset"></a>

## Ordering, Grouping, Limit and Offset

<a name="ordering"></a>

### Ordering

<a name="orderby"></a>

#### The `orderBy` Method


`orderBy` メソッドを使用すると、クエリの結果を特定の列で並べ替えることができます。 `orderBy` メソッドで受け入れられる最初の引数は並べ替えの基準となる列である必要があり、2 番目の引数は並べ替えの方向を決定し、`asc` または `desc` のいずれかになります。

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


複数の列で並べ替えるには、必要なだけ `orderBy` を呼び出すだけです。

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


並べ替え方向はオプションであり、デフォルトでは昇順です。降順で並べ替える場合は、`orderBy` メソッドの 2 番目のパラメーターを指定するか、単に `orderByDesc` を使用します。

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


最後に、`->` 演算子を使用して、結果を JSON 列内の値で並べ替えることができます。

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

<a name="latest-oldest"></a>

#### The `latest` and `oldest` Methods


`latest` メソッドと `oldest` メソッドを使用すると、結果を日付順に簡単に並べることができます。デフォルトでは、結果はテーブルの `created_at` 列によって並べられます。または、並べ替えの基準にする列名を渡すこともできます。

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

<a name="sequence-ordering"></a>

#### Sequence Ordering


`inOrderOf` メソッドを使うと、文字列や enum などの値の並び順に従ってクエリ結果を並べ替えられます。

```php
$orders = DB::table('orders')
    ->inOrderOf('status', [OrderStatus::Shipped, OrderStatus::Delivered, OrderStatus::Cancelled])
    ->get();
```

<a name="random-ordering"></a>

#### Random Ordering


`inRandomOrder` メソッドを使用して、クエリ結果をランダムに並べ替えることができます。たとえば、このメソッドを使用してランダムなユーザーを取得できます。

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

<a name="removing-existing-orderings"></a>

#### Removing Existing Orderings


`reorder` メソッドは、以前にクエリに適用されたすべての "order by" 句を削除します。

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

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


既存の「order by」句をすべて削除し、まったく新しい順序をクエリに適用するために、`reorder` メソッドを呼び出すときに列と方向を渡すことができます。

```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();
```

<a name="grouping"></a>

### Grouping

<a name="groupby-having"></a>

#### The `groupBy` and `having` Methods


ご想像のとおり、`groupBy` メソッドと `having` メソッドを使用してクエリ結果をグループ化できます。 `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](#raw-methods) メソッドを参照してください。

<a name="limit-and-offset"></a>

### Limit and Offset


`limit` メソッドと `offset` メソッドを使用して、クエリから返される結果の数を制限したり、クエリ内の指定された数の結果をスキップしたりできます。

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

<a name="conditional-clauses"></a>

## Conditional Clauses


場合によっては、特定のクエリ句を別の条件に基づいてクエリに適用したい場合があります。たとえば、受信 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` の場合、クロージャは実行されません。したがって、上記の例では、`when` メソッドに指定されたクロージャは、受信リクエストに `role` フィールドが存在し、`true` と評価される場合にのみ呼び出されます。


別のクロージャを `when` メソッドの 3 番目の引数として渡すことができます。このクロージャは、最初の引数が `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();
```

<a name="insert-statements"></a>

## Insert Statements


クエリビルダは、データベース テーブルにレコードを挿入するために使用できる `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` は [bypass MySQL's strict mode](https://dev.mysql.com/doc/refman/en/sql-mode.html#ignore-effect-on-execution) になります。

```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)));
```

<a name="auto-incrementing-ids"></a>

#### Auto-Incrementing IDs


テーブルに自動インクリメント ID がある場合は、`insertGetId` メソッドを使用してレコードを挿入し、ID を取得します。

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

> [!WARNING]
> PostgreSQLを使用する場合、`insertGetId` メソッドは自動インクリメントするカラムの名前が `id` であることを前提とします。別の「sequence」からIDを取得する場合は、`insertGetId` メソッドの第2引数としてカラム名を渡してください。

<a name="upserts"></a>

### Upserts


`upsert` メソッドは、存在しないレコードを挿入し、指定した新しい値で既存のレコードを更新します。メソッドの最初の引数は挿入または更新する値で構成され、2 番目の引数は関連するテーブル内のレコードを一意に識別する列をリストします。このメソッドの 3 番目と最後の引数は、一致するレコードがデータベースにすでに存在する場合に更新する必要がある列の配列です。

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


上の例では、Laravel は 2 つのレコードを挿入しようとします。同じ `departure` 列値と `destination` 列値を持つレコードがすでに存在する場合、Laravel はそのレコードの `price` 列を更新します。

> [!WARNING]
> SQL Server 以外のすべてのデータベースでは、`upsert` メソッドの第2引数に指定するカラムに「primary」または「unique」インデックスが必要です。また、MariaDB と MySQL のデータベースドライバは `upsert` メソッドの第2引数を無視し、既存レコードの検出にはテーブルの「primary」および「unique」インデックスを常に使用します。

<a name="update-statements"></a>

## Update Statements


クエリビルダは、データベースにレコードを挿入するだけでなく、`update` メソッドを使用して既存のレコードを更新することもできます。 `update` メソッドは、`insert` メソッドと同様に、更新される列を示す列と値のペアの配列を受け入れます。 `update` メソッドは、影響を受ける行の数を返します。 `where` 句を使用して、`update` クエリを制約できます。

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

<a name="update-or-insert"></a>

#### Update or Insert


場合によっては、データベース内の既存のレコードを更新したり、一致するレコードが存在しない場合にレコードを作成したりすることが必要な場合があります。このシナリオでは、`updateOrInsert` メソッドが使用される可能性があります。 `updateOrInsert` メソッドは、レコードを検索するための条件の配列と、更新される列を示す列と値のペアの配列という 2 つの引数を受け入れます。


`updateOrInsert` メソッドは、最初の引数の列と値のペアを使用して、一致するデータベース レコードの検索を試みます。レコードが存在する場合は、2 番目の引数の値で更新されます。レコードが見つからない場合は、両方の引数の属性を結合した新しいレコードが挿入されます。

```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,
    ],
);
```

<a name="updating-json-columns"></a>

### Updating JSON Columns


JSON 列を更新するときは、`->` 構文を使用して、JSON オブジェクト内の適切なキーを更新する必要があります。この操作は、MariaDB 10.3 以降、MySQL 5.7 以降、および PostgreSQL 9.5 以降でサポートされています。

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

<a name="increment-and-decrement"></a>

### Increment and Decrement


クエリビルダは、特定の列の値を増減する便利なメソッドも提供します。これらのメソッドは両方とも、少なくとも 1 つの引数、つまり変更する列を受け入れます。 2 番目の引数を指定して、列を増分または減分する量を指定できます。

```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']);
```


さらに、`incrementEach` および `decrementEach` メソッドを使用して、複数の列を一度に増加または減少させることができます。

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

<a name="delete-statements"></a>

## Delete Statements


クエリビルダの `delete` メソッドを使用して、テーブルからレコードを削除できます。 `delete` メソッドは、影響を受ける行の数を返します。 `delete` メソッドを呼び出す前に「where」句を追加することで、`delete` ステートメントを制約できます。

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

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

<a name="pessimistic-locking"></a>

## Pessimistic Locking


クエリビルダには、`select` ステートメントの実行時に「悲観的ロック」を実現するのに役立つ関数もいくつか含まれています。 「共有ロック」を使用してステートメントを実行するには、`sharedLock` メソッドを呼び出すことができます。共有ロックにより、トランザクションがコミットされるまで、選択された行は変更されなくなります。

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


あるいは、`lockForUpdate` メソッドを使用することもできます。 「更新用」ロックは、選択されたレコードが変更されたり、別の共有ロックで選択されたりすることを防ぎます。

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


必須ではありませんが、悲観的ロックは [transaction](/ja/php/docs/database/#database-transactions) 内で使用することをおすすめします。これにより、処理全体が完了するまで、取得したデータがデータベース内で変更されないことが保証されます。処理に失敗した場合、トランザクションが変更をロールバックし、ロックも自動的に解放します。

```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
        ]);
});
```

<a name="reusable-query-components"></a>

## Reusable Query Components


アプリケーション全体でクエリ ロジックを繰り返している場合は、クエリビルダの `tap` メソッドと `pipe` メソッドを使用して、ロジックを再利用可能なオブジェクトに抽出できます。アプリケーションに次の 2 つの異なるクエリがあると想像してください。

```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();
```

<a name="query-pipes"></a>

#### Query Pipes


`tap` メソッドは常にクエリビルダを返します。クエリを実行して別の値を返すオブジェクトを抽出したい場合は、代わりに `pipe` メソッドを使用できます。


アプリケーション全体で共有する [pagination](/ja/php/docs/pagination/) ロジックを含む、次のクエリオブジェクトを考えてみましょう。クエリに条件を適用する `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);
```

<a name="debugging"></a>

## Debugging


クエリの構築中に `dd` メソッドと `dump` メソッドを使用して、現在のクエリ バインディングと SQL をダンプできます。 `dd` メソッドはデバッグ情報を表示し、リクエストの実行を停止します。 `dump` メソッドはデバッグ情報を表示しますが、リクエストの実行は継続できます。

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

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


`dumpRawSql` メソッドと `ddRawSql` メソッドをクエリで呼び出して、すべてのパラメータ バインディングが適切に置換されたクエリの SQL をダンプできます。

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

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