Readouble

Laravel 8.x データベース:準備

イントロダクションIntroduction

最近のほとんどすべてのWebアプリケーションは、データベースとやり取りします。Laravelでは、素のSQL、fluent(流暢な)クエリビルダ、およびEloquent ORMを使用して、サポートしているさまざまなでデータベースとのやり取りを非常に簡単にしています。現在、Laravelは5つのデータベースをファーストパーティサポートしています。Almost every modern web application interacts with a database. Laravel makes interacting with databases extremely simple across a variety of supported databases using raw SQL, a fluent query builder[/docs/{{version}}/queries], and the Eloquent ORM[/docs/{{version}}/eloquent]. Currently, Laravel provides first-party support for five databases:

  • MariaDB10.2以上 (バージョンポリシー)MariaDB 10.2 (Version Policy[https://mariadb.org/about/#maintenance-policy])
  • MySQL5.7以上 (バージョンポリシー)MySQL 5.7 (Version Policy[https://en.wikipedia.org/wiki/MySQL#Release_history])
  • PostgreSQL9.6以上 (バージョンポリシー)PostgreSQL 9.6 (Version Policy[https://www.postgresql.org/support/versioning/])
  • SQLite3.8.8以上SQLite 3.8.8
  • SQL Server2017以上 (バージョンポリシー)SQL Server 2017 (Version Policy[https://docs.microsoft.com/en-us/lifecycle/products/?products=sql-server])

設定Configuration

Laravelのデータベースサービスの設定は、アプリケーションのconfig/database.php設定ファイルにあります。このファイルは、全データベース接続を定義し、デフォルトで使用する接続を指定できます。このファイル内のほとんどの設定オプションは、アプリケーションの環境変数の値によって決まります。Laravelがサポートしているデータベースシステムのほとんどの設定例をこのファイルに用意しています。The configuration for Laravel's database services is located in your application's config/database.php configuration file. In this file, you may define all of your database connections, as well as specify which connection should be used by default. Most of the configuration options within this file are driven by the values of your application's environment variables. Examples for most of Laravel's supported database systems are provided in this file.

デフォルトのLaravelのサンプル環境設定は、Laravel Sailで使用できるようになっています。SailはローカルマシンでLaravelアプリケーションを開発するためのDocker環境です。しかし、ローカルデータベースの必要に応じ、データベース設定を自由に変更してください。By default, Laravel's sample environment configuration[/docs/{{version}}/configuration#environment-configuration] is ready to use with Laravel Sail[/docs/{{version}}/sail], which is a Docker configuration for developing Laravel applications on your local machine. However, you are free to modify your database configuration as needed for your local database.

SQLite設定SQLite Configuration

SQLiteデータベースは、ファイルシステム上の単一ファイルに含まれます。ターミナルでtouchコマンドを使用して新しいSQLiteデータベースを作成できます。(touch database/database.sqlite)データベースを作成したあと、データベースへの絶対パスをDB_DATABASE環境変数で指定することにより、このデータベースを使用するよう簡単に設定できます。SQLite databases are contained within a single file on your filesystem. You can create a new SQLite database using the touch command in your terminal: touch database/database.sqlite. After the database has been created, you may easily configure your environment variables to point to this database by placing the absolute path to the database in the DB_DATABASE environment variable:

DB_CONNECTION=sqlite
DB_DATABASE=/absolute/path/to/database.sqlite

SQLite接続の外部キー制約を有効にするには、DB_FOREIGN_KEYS環境変数をtrueに設定する必要があります。To enable foreign key constraints for SQLite connections, you should set the DB_FOREIGN_KEYS environment variable to true:

DB_FOREIGN_KEYS=true

Microsoft SQLサーバ設定Microsoft SQL Server Configuration

Microsoft SQL Serverデータベースを使用するには、sqlsrvpdo_sqlsrvPHP拡張機能と、Microsoft SQL ODBCドライバーなど必要な依存関係パッケージを確実にインストールしてください。To use a Microsoft SQL Server database, you should ensure that you have the sqlsrv and pdo_sqlsrv PHP extensions installed as well as any dependencies they may require such as the Microsoft SQL ODBC driver.

URLを使用した設定Configuration Using URLs

通常、データベース接続は、hostdatabaseusernamepasswordなどの複数の設定値により構成します。こうした設定値には、それぞれ対応する環境変数があります。これは、運用サーバでデータベース接続情報を設定するときに、複数の環境変数を管理する必要があることを意味します。Typically, database connections are configured using multiple configuration values such as host, database, username, password, etc. Each of these configuration values has its own corresponding environment variable. This means that when configuring your database connection information on a production server, you need to manage several environment variables.

AWSやHerokuなどの一部のマネージドデータベースプロバイダは、データベースのすべての接続情報を単一の文字カラムで含む単一のデータベース「URL」を提供しています。データベースURLの例は、次のようになります。Some managed database providers such as AWS and Heroku provide a single database "URL" that contains all of the connection information for the database in a single string. An example database URL may look something like the following:

mysql://root:password@127.0.0.1/forge?charset=UTF-8

こうしたURLは通常、標準のスキーマ規約に従います。These URLs typically follow a standard schema convention:

driver://username:password@host:port/database?options

利便性のため、Laravelは複数の設定オプションを使用してデータベースを構成する代わりに、こうしたURLをサポートしています。url(または対応するDATABASE_URL環境変数)設定オプションが存在する場合は、データベース接続と接続情報を抽出するためにそれを使用します。For convenience, Laravel supports these URLs as an alternative to configuring your database with multiple configuration options. If the url (or corresponding DATABASE_URL environment variable) configuration option is present, it will be used to extract the database connection and credential information.

読み/書き接続Read & Write Connections

SELECTステートメントに1つのデータベース接続を使用し、INSERT、UPDATE、およびDELETEステートメントに別のデータベース接続を使用したい場合があるでしょう。Laravelでは簡単に、素のクエリ、クエリビルダ、もしくはEloquent ORMのいずれを使用していても常に適切な接続が使用されます。Sometimes you may wish to use one database connection for SELECT statements, and another for INSERT, UPDATE, and DELETE statements. Laravel makes this a breeze, and the proper connections will always be used whether you are using raw queries, the query builder, or the Eloquent ORM.

読み取り/書き込み接続を設定する方法を確認するため、以下の例を見てみましょう。To see how read / write connections should be configured, let's look at this example:

'mysql' => [
    'read' => [
        'host' => [
            '192.168.1.1',
            '196.168.1.2',
        ],
    ],
    'write' => [
        'host' => [
            '196.168.1.3',
        ],
    ],
    'sticky' => true,
    'driver' => 'mysql',
    'database' => 'database',
    'username' => 'root',
    'password' => '',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
],

設定配列には、readwritestickyの3キーが追加されていることに注目してください。readキーとwriteキーには、単一のキーとしてhostを含む配列値があります。readおよびwrite接続の残りのデータベースオプションは、メインのmysql設定配列からマージされます。Note that three keys have been added to the configuration array: read, write and sticky. The read and write keys have array values containing a single key: host. The rest of the database options for the read and write connections will be merged from the main mysql configuration array.

メインのmysql配列の値をオーバーライドする場合にのみ、read配列とwrite配列へ項目を配置する必要があります。したがって、この場合、192.168.1.1は「読み取り」接続のホストとして使用し、192.168.1.3は「書き込み」接続に使用します。データベースの接続情報、プレフィックス、文字セット、およびメインのmysql配列内の他のすべてのオプションは、両方の接続で共有されます。host設定配列に複数の値が存在する場合、リクエストごとランダムにデータベースホストを選択します。You only need to place items in the read and write arrays if you wish to override the values from the main mysql array. So, in this case, 192.168.1.1 will be used as the host for the "read" connection, while 192.168.1.3 will be used for the "write" connection. The database credentials, prefix, character set, and all other options in the main mysql array will be shared across both connections. When multiple values exist in the host configuration array, a database host will be randomly chosen for each request.

stickyオプションThe sticky Option

stickyオプションは、現在のリクエストサイクル中にデータベースへ書き込まれたレコードをすぐに読み取るため使用するオプション値です。stickyオプションが有効になっており、現在のリクエストサイクル中にデータベースへ対し「書き込み」操作が実行された場合、それ以降の「読み取り」操作では「書き込み」接続が使用されます。これにより、要求サイクル中に書き込まれたデータを、同じ要求中にデータベースからすぐに読み戻すことができます。これがアプリケーションにとって望ましい動作であるかどうかを判断するのは使用者の皆さん次第です。The sticky option is an optional value that can be used to allow the immediate reading of records that have been written to the database during the current request cycle. If the sticky option is enabled and a "write" operation has been performed against the database during the current request cycle, any further "read" operations will use the "write" connection. This ensures that any data written during the request cycle can be immediately read back from the database during that same request. It is up to you to decide if this is the desired behavior for your application.

SQLクエリの実行Running SQL Queries

データベース接続を設定したら、DBファサードを使用してクエリが実行できます。DBファサードは、クエリのタイプごとにselectupdateinsertdeletestatementメソッドを提供しています。Once you have configured your database connection, you may run queries using the DB facade. The DB facade provides methods for each type of query: select, update, insert, delete, and statement.

SELECTクエリの実行Running A Select Query

基本的なSELECTクエリを実行するには、DBファサードでselectメソッドを使用します。To run a basic SELECT query, you may use the select method on the DB facade:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
    /**
     * アプリケーションの全ユーザーのリストを表示
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = DB::select('select * from users where active = ?', [1]);

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

selectメソッドの最初の引数はSQLクエリであり、2番目の引数はクエリにバインドする必要のあるパラメータバインディングです。通常、これらは where句の制約の値です。パラメータバインディングは、SQLインジェクションに対する保護を提供します。The first argument passed to the select method is the SQL query, while the second argument is any parameter bindings that need to be bound to the query. Typically, these are the values of the where clause constraints. Parameter binding provides protection against SQL injection.

selectメソッドは常に結果の配列を返します。配列内の各結果は、データベースのレコードを表すPHPのstdClassオブジェクトになります。The select method will always return an array of results. Each result within the array will be a PHP stdClass object representing a record from the database:

use Illuminate\Support\Facades\DB;

$users = DB::select('select * from users');

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

名前付きバインディングの使用Using Named Bindings

パラメータバインディングを表すために?を使用する代わりに、名前付きバインディングを使用してクエリを実行できます。Instead of using ? to represent your parameter bindings, you may execute a query using named bindings:

$results = DB::select('select * from users where id = :id', ['id' => 1]);

INSERT文の実行Running An Insert Statement

insertステートメントを実行するには、DBファサードでinsertメソッドを使用します。selectと同様に、このメソッドはSQLクエリを最初の引数に取り、バインディングを2番目の引数に取ります。To execute an insert statement, you may use the insert method on the DB facade. Like select, this method accepts the SQL query as its first argument and bindings as its second argument:

use Illuminate\Support\Facades\DB;

DB::insert('insert into users (id, name) values (?, ?)', [1, 'Marc']);

更新文の実行Running An Update Statement

データベース内の既存のレコードを更新するには、updateメソッドを使用する必要があります。メソッドは実行の影響を受けた行数を返します。The update method should be used to update existing records in the database. The number of rows affected by the statement is returned by the method:

use Illuminate\Support\Facades\DB;

$affected = DB::update(
    'update users set votes = 100 where name = ?',
    ['Anita']
);

DELETE文の実行Running A Delete Statement

データベースからレコードを削除するには、deleteメソッドを使用する必要があります。updateと同様に、メソッドは影響を受けた行数を返します。The delete method should be used to delete records from the database. Like update, the number of rows affected will be returned by the method:

use Illuminate\Support\Facades\DB;

$deleted = DB::delete('delete from users');

一般的な文の実行Running A General Statement

一部のデータベース操作文は値を返しません。こうしたタイプの操作では、DBファサードでstatementメソッドを使用します。Some database statements do not return any value. For these types of operations, you may use the statement method on the DB facade:

DB::statement('drop table users');

プリペアドではない文の実行Running An Unprepared Statement

値をバインドせずSQL文を実行したい場合があります。それには、DBファサードのunpreparedメソッドを使用します。Sometimes you may want to execute an SQL statement without binding any values. You may use the DB facade's unprepared method to accomplish this:

DB::unprepared('update users set votes = 100 where name = "Dries"');

Note: note プリペアドではない文はパラメーターをバインドしないため、SQLインジェクションに対して脆弱である可能性があります。プリペアドではない文内では、ユーザーの値のコントロールを許可しないでください。{note} Since unprepared statements do not bind parameters, they may be vulnerable to SQL injection. You should never allow user controlled values within an unprepared statement.

暗黙のコミットImplicit Commits

トランザクション内でDBファサードのstatementおよびunpreparedメソッドを使用する場合、暗黙のコミットを引き起こすステートメントを回避するように注意する必要があります。これらのステートメントにより、データベースエンジンはトランザクション全体を間接的にコミットし、Laravelはデータベースのトランザクションレベルを認識しなくなります。このようなステートメントの例は、データベーステーブルの作成です。When using the DB facade's statement and unprepared methods within transactions you must be careful to avoid statements that cause implicit commits[https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html]. These statements will cause the database engine to indirectly commit the entire transaction, leaving Laravel unaware of the database's transaction level. An example of such a statement is creating a database table:

DB::unprepared('create table a (col varchar(1) null)');

暗黙的なコミットを引き起こす、すべてのステートメントのリストは、MySQLのマニュアルを参照してください。Please refer to the MySQL manual for a list of all statements[https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html] that trigger implicit commits.

複数データベース接続の使用Using Multiple Database Connections

アプリケーションがconfig/database.php設定ファイルで複数の接続を定義している場合、DBファサードが提供するconnectionメソッドを介して各接続にアクセスできます。connectionメソッドに渡される接続名は、config/database.php設定ファイルにリストしている接続、または実行時にconfigヘルパを使用して設定した接続の1つに対応している必要があります。If your application defines multiple connections in your config/database.php configuration file, you may access each connection via the connection method provided by the DB facade. The connection name passed to the connection method should correspond to one of the connections listed in your config/database.php configuration file or configured at runtime using the config helper:

use Illuminate\Support\Facades\DB;

$users = DB::connection('sqlite')->select(...);

接続インスタンスでgetPdoメソッドを使用して、接続の基になる素のPDOインスタンスにアクセスできます。You may access the raw, underlying PDO instance of a connection using the getPdo method on a connection instance:

$pdo = DB::connection()->getPdo();

クエリイベントのリッスンListening For Query Events

アプリケーションが実行するSQLクエリごとに呼び出すクロージャを指定する場合は、DBファサードのlistenメソッドを使用します。このメソッドは、クエリのログ記録やデバッグに役立ちます。クエリリスナクロージャは、サービスプロバイダbootメソッドで登録します。If you would like to specify a closure that is invoked for each SQL query executed by your application, you may use the DB facade's listen method. This method can be useful for logging queries or debugging. You may register your query listener closure in the boot method of a service provider[/docs/{{version}}/providers]:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * アプリケーションの全サービスの登録
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * アプリケーションの全サービスの起動初期処理
     *
     * @return void
     */
    public function boot()
    {
        DB::listen(function ($query) {
            // $query->sql;
            // $query->bindings;
            // $query->time;
        });
    }
}

データベーストランザクションDatabase Transactions

DBファサードが提供するtransactionメソッドを使用して、データベーストランザクション内で一連の操作を実行できます。トランザクションクロージャ内で例外が投げられた場合、トランザクションを自動的にロールバックし、その例外を再度投げます。クロージャが正常に実行されると、トランザクションを自動的にコミットします。transactionメソッドの使用中にロールバックやコミットを手動で実行する心配はありません。You may use the transaction method provided by the DB facade to run a set of operations within a database transaction. If an exception is thrown within the transaction closure, the transaction will automatically be rolled back and the exception is re-thrown. If the closure executes successfully, the transaction will automatically be committed. You don't need to worry about manually rolling back or committing while using the transaction method:

use Illuminate\Support\Facades\DB;

DB::transaction(function () {
    DB::update('update users set votes = 1');

    DB::delete('delete from posts');
});

デッドロックの処理Handling Deadlocks

transactionメソッドは、デッドロックが発生したときにトランザクションを再試行する回数をオプションとして、2番目の引数に取ります。試行回数が終了した場合は、例外を投げます。The transaction method accepts an optional second argument which defines the number of times a transaction should be retried when a deadlock occurs. Once these attempts have been exhausted, an exception will be thrown:

use Illuminate\Support\Facades\DB;

DB::transaction(function () {
    DB::update('update users set votes = 1');

    DB::delete('delete from posts');
}, 5);

トランザクションを手動で使用Manually Using Transactions

トランザクションを手動で開始し、ロールバックとコミットを自分で完全にコントロールしたい場合は、DBファサードが提供するbeginTransactionメソッドを使用します。If you would like to begin a transaction manually and have complete control over rollbacks and commits, you may use the beginTransaction method provided by the DB facade:

use Illuminate\Support\Facades\DB;

DB::beginTransaction();

rollBackメソッドにより、トランザクションをロールバックできます。You can rollback the transaction via the rollBack method:

DB::rollBack();

commitメソッドにより、トランザクションをコミットできます。Lastly, you can commit a transaction via the commit method:

DB::commit();

lightbulb">Tip!! DBファサードのトランザクションメソッドは、クエリビルダEloquent ORMの両方のトランザクションを制御します。{tip} The DB facade's transaction methods control the transactions for both the query builder[/docs/{{version}}/queries] and Eloquent ORM[/docs/{{version}}/eloquent].

データベースCLIへの接続Connecting To The Database CLI

データベースのCLIに接続する場合は、db Artisanコマンドを使用します。If you would like to connect to your database's CLI, you may use the db Artisan command:

php artisan db

必要に応じて、データベース接続名を指定して、デフォルト接続以外のデータベースへ接続できます。If needed, you may specify a database connection name to connect to a database connection that is not the default connection:

php artisan db mysql

章選択

設定

明暗テーマ
light_mode
dark_mode
brightness_auto システム設定に合わせる
テーマ選択
photo_size_select_actual デフォルト
photo_size_select_actual モノクローム(白黒)
photo_size_select_actual Solarized風
photo_size_select_actual GitHub風(青ベース)
photo_size_select_actual Viva(黄緑ベース)
photo_size_select_actual Happy(紫ベース)
photo_size_select_actual Mint(緑ベース)
コードハイライトテーマ選択

明暗テーマごとに、コードハイライトのテーマを指定できます。

テーマ配色確認
スクリーン表示幅
640px
80%
90%
100%

768px以上の幅があるときのドキュメント部分表示幅です。

インデント
無し
1rem
2rem
3rem
原文確認
原文を全行表示
原文を一行ずつ表示
使用しない

※ 段落末のEボタンへカーソルオンで原文をPopupします。

Diff表示形式
色分けのみで区別
行頭の±で区別
削除線と追記で区別

※ [tl!…]形式の挿入削除行の表示形式です。

Pagination和文
ペジネーション
ペギネーション
ページネーション
ページ付け
Scaffold和文
スカフォールド
スキャフォールド
型枠生成
本文フォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

コードフォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

保存内容リセット

localStrageに保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作