Readouble

Laravel 7.x Eloquent:利用の開始

イントロダクションIntroduction

Eloquent ORMはLaravelに含まれている、美しくシンプルなアクティブレコードによるデーター操作の実装です。それぞれのデータベーステーブルは関連する「モデル」と結びついています。モデルによりテーブル中のデータをクエリできますし、さらに新しいレコードを追加することもできます。The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

使用開始前にconfig/database.phpを確実に設定してください。データベースの詳細はドキュメントで確認してください。Before getting started, be sure to configure a database connection in config/database.php. For more information on configuring your database, check out the documentation[/docs/{{version}}/database#configuration].

モデル定義Defining Models

利用を開始するには、まずEloquentモデルを作成しましょう。通常モデルはappディレクトリ下に置きますが、composer.jsonファイルでオートロードするように指定した場所であれば、どこでも自由に設置できます。すべてのEloquentモデルは、Illuminate\Database\Eloquent\Modelを拡張する必要があります。To get started, let's create an Eloquent model. Models typically live in the app directory, but you are free to place them anywhere that can be auto-loaded according to your composer.json file. All Eloquent models extend Illuminate\Database\Eloquent\Model class.

モデルを作成する一番簡単な方法はmake:model Artisanコマンドを使用することです。The easiest way to create a model instance is using the make:model Artisan command[/docs/{{version}}/artisan]:

php artisan make:model Flight

モデル作成時にデータベースマイグレーションも生成したければ、--migration-mオプションを使ってください。If you would like to generate a database migration[/docs/{{version}}/migrations] when you generate the model, you may use the --migration or -m option:

php artisan make:model Flight --migration

php artisan make:model Flight -m

Eloquentモデル規約Eloquent Model Conventions

ではflightsデータベーステーブルに情報を保存し、取得するために使用するFlightモデルクラスを例として見てください。Now, let's look at an example Flight model, which we will use to retrieve and store information from our flights database table:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    //
}

テーブル名Table Names

Flightモデルにどのテーブルを使用するか、Eloquentに指定していない点に注目してください。他の名前を明示的に指定しない限り、クラス名を複数形の「スネークケース」にしたものが、テーブル名として使用されます。今回の例で、EloquentはFlightモデルをflightsテーブルに保存します。モデルのtableプロパティを定義し、カスタムテーブル名を指定することもできます。Note that we did not tell Eloquent which table to use for our Flight model. By convention, the "snake case", plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the Flight model stores records in the flights table. You may specify a custom table by defining a table property on your model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * モデルと関連しているテーブル
     *
     * @var string
     */
    protected $table = 'my_flights';
}

主キーPrimary Keys

Eloquentはさらにテーブルの主キーがidというカラム名であると想定しています。この規約をオーバーライドする場合は、protectedのprimaryKeyプロパティを定義してください。Eloquent will also assume that each table has a primary key column named id. You may define a protected $primaryKey property to override this convention:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * テーブルの主キー
     *
     * @var string
     */
    protected $primaryKey = 'flight_id';
}

さらに、Eloquentは主キーを自動増分される整数値であるとも想定しています。つまり、デフォルト状態で主キーは自動的にintへキャストされます。自動増分ではない、もしくは整数値ではない主キーを使う場合、モデルにpublicの$incrementingプロパティを用意し、falseをセットしてください。In addition, Eloquent assumes that the primary key is an incrementing integer value, which means that by default the primary key will automatically be cast to an int. If you wish to use a non-incrementing or a non-numeric primary key you must set the public $incrementing property on your model to false:

<?php

class Flight extends Model
{
    /**
     * IDが自動増分されるか
     *
     * @var bool
     */
    public $incrementing = false;
}

主キーが整数でない場合は、モデルのprotectedの$keyTypeプロパティへstring値を設定してください。If your primary key is not an integer, you should set the protected $keyType property on your model to string:

<?php

class Flight extends Model
{
    /**
     * 自動増分IDの「タイプ」
     *
     * @var string
     */
    protected $keyType = 'string';
}

タイムスタンプTimestamps

デフォルトでEloquentはデータベース上に存在するcreated_at(作成時間)とupdated_at(更新時間)カラムを自動的に更新します。これらのカラムの自動更新をEloquentにしてほしくない場合は、モデルの$timestampsプロパティをfalseに設定してください。By default, Eloquent expects created_at and updated_at columns to exist on your tables. If you do not wish to have these columns automatically managed by Eloquent, set the $timestamps property on your model to false:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * モデルのタイムスタンプを更新するかの指示
     *
     * @var bool
     */
    public $timestamps = false;
}

タイムスタンプのフォーマットをカスタマイズする必要があるなら、モデルの$dateFormatプロパティを設定してください。このプロパティはデータベースに保存される日付属性のフォーマットを決めるために使用されると同時に、配列やJSONへシリアライズする時にも使われます。If you need to customize the format of your timestamps, set the $dateFormat property on your model. This property determines how date attributes are stored in the database, as well as their format when the model is serialized to an array or JSON:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * モデルの日付カラムの保存フォーマット
     *
     * @var string
     */
    protected $dateFormat = 'U';
}

タイムスタンプを保存するカラム名をカスタマイズする必要がある場合、モデルにCREATED_ATUPDATED_AT定数を設定してください。If you need to customize the names of the columns used to store the timestamps, you may set the CREATED_AT and UPDATED_AT constants in your model:

<?php

class Flight extends Model
{
    const CREATED_AT = 'creation_date';
    const UPDATED_AT = 'last_update';
}

データベース接続Database Connection

Eloquentモデルはデフォルトとして、アプリケーションに設定されているデフォルトのデータベース接続を使用します。モデルで異なった接続を指定したい場合は、$connectionプロパティを使用します。By default, all Eloquent models will use the default database connection configured for your application. If you would like to specify a different connection for the model, use the $connection property:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * モデルで使用するコネクション名
     *
     * @var string
     */
    protected $connection = 'connection-name';
}

デフォルト属性値Default Attribute Values

あるモデルの属性にデフォルト値を指定したい場合は、そのモデルに$attributesプロパティを定義してください。If you would like to define the default values for some of your model's attributes, you may define an $attributes property on your model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * 属性に対するモデルのデフォルト値
     *
     * @var array
     */
    protected $attributes = [
        'delayed' => false,
    ];
}

モデルの取得Retrieving Models

モデルと対応するデータベーステーブルを作成したら、データベースからデータを取得できるようになりました。各Eloquentモデルは、対応するデータベーステーブルへすらすらとクエリできるようにしてくれるクエリビルダだと考えてください。例を見てください。Once you have created a model and its associated database table[/docs/{{version}}/migrations#writing-migrations], you are ready to start retrieving data from your database. Think of each Eloquent model as a powerful query builder[/docs/{{version}}/queries] allowing you to fluently query the database table associated with the model. For example:

<?php

$flights = App\Flight::all();

foreach ($flights as $flight) {
    echo $flight->name;
}

制約の追加Adding Additional Constraints

Eloquentのallメソッドはモデルテーブルの全レコードを結果として返します。Eloquentモデルはクエリビルダとしても動作しますのでクエリに制約を付け加えることもでき、結果を取得するにはgetメソッドを使用します。The Eloquent all method will return all of the results in the model's table. Since each Eloquent model serves as a query builder[/docs/{{version}}/queries], you may also add constraints to queries, and then use the get method to retrieve the results:

$flights = App\Flight::where('active', 1)
               ->orderBy('name', 'desc')
               ->take(10)
               ->get();

lightbulb">Tip!! Eloquentモデルはクエリビルダですから、クエリビルダで使用できる全メソッドを確認しておくべきでしょう。Eloquentクエリでどんなメソッドも使用できます。{tip} Since Eloquent models are query builders, you should review all of the methods available on the query builder[/docs/{{version}}/queries]. You may use any of these methods in your Eloquent queries.

モデルのリフレッシュRefreshing Models

freshrefreshメソッドを使用し、モデルをリフレッシュできます。freshメソッドはデータベースからモデルを再取得します。既存のモデルインスタンスは影響を受けません。You can refresh models using the fresh and refresh methods. The fresh method will re-retrieve the model from the database. The existing model instance will not be affected:

$flight = App\Flight::where('number', 'FR 900')->first();

$freshFlight = $flight->fresh();

refreshメソッドは、データベースから取得したばかりのデータを使用し、既存のモデルを再構築します。The refresh method will re-hydrate the existing model using fresh data from the database. In addition, all of its loaded relationships will be refreshed as well:

$flight = App\Flight::where('number', 'FR 900')->first();

$flight->number = 'FR 456';

$flight->refresh();

$flight->number; // "FR 900"

コレクションCollections

複数の結果を取得するallgetのようなEloquentメソッドは、Illuminate\Database\Eloquent\Collectionインスタンスを返します。CollectionクラスはEloquent結果を操作する多くの便利なクラスを提供しています。For Eloquent methods like all and get which retrieve multiple results, an instance of Illuminate\Database\Eloquent\Collection will be returned. The Collection class provides a variety of helpful methods[/docs/{{version}}/eloquent-collections#available-methods] for working with your Eloquent results:

$flights = $flights->reject(function ($flight) {
    return $flight->cancelled;
});

このコレクションは配列のようにループさせることもできます。You may also loop over the collection like an array:

foreach ($flights as $flight) {
    echo $flight->name;
}

結果の分割Chunking Results

数千のEloquentレコードを処理する必要がある場合はchunkコマンドを利用してください。chunkメソッドはEloquentモデルの「塊(chunk)」を取得し、引数の「クロージャ」に渡します。chunkメソッドを使えば大きな結果を操作するときのメモリを節約できます。If you need to process thousands of Eloquent records, use the chunk command. The chunk method will retrieve a "chunk" of Eloquent models, feeding them to a given Closure for processing. Using the chunk method will conserve memory when working with large result sets:

Flight::chunk(200, function ($flights) {
    foreach ($flights as $flight) {
        //
    }
});

最初の引数には「チャンク(塊)」ごとにいくつのレコードを処理するかを渡します。2番めの引数にはクロージャを渡し、そのデータベースからの結果をチャンクごとに処理するコードを記述します。クロージャへ渡されるチャンクを取得するたびに、データベースクエリは実行されます。The first argument passed to the method is the number of records you wish to receive per "chunk". The Closure passed as the second argument will be called for each chunk that is retrieved from the database. A database query will be executed to retrieve each chunk of records passed to the Closure.

カーソルの使用Using Cursors

cursorメソッドにより、ひとつだけクエリを実行するカーソルを使用し、データベース全体を繰り返し処理できます。大量のデータを処理する場合、cursorメソッドを使用すると、大幅にメモリ使用量を減らせるでしょう。The cursor method allows you to iterate through your database records using a cursor, which will only execute a single query. When processing large amounts of data, the cursor method may be used to greatly reduce your memory usage:

foreach (Flight::where('foo', 'bar')->cursor() as $flight) {
    //
}

cursorIlluminate\Support\LazyCollectionインスタンスを返します。レイジーコレクションにより、Laravelの典型的なコレクションで使用可能なメソッドを使用しながらも、一度に1つのモデルだけをメモリへロードします。The cursor returns an Illuminate\Support\LazyCollection instance. Lazy collections[/docs/{{version}}/collections#lazy-collections] allow you to use many of collection methods available on typical Laravel collections while only loading a single model into memory at a time:

$users = App\User::cursor()->filter(function ($user) {
    return $user->id > 500;
});

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

上級のサブクエリAdvanced Subqueries

SELECTのサブクエリSubquery Selects

1回のクエリで関連テーブルから情報を取得する上級サブクエリもEloquentはサポートしています。例として、フライト(flights)と目的地(destinations)テーブルを想像してください。flightsテーブルは、フライトの目的地への到着時間を意味するarrived_atカラムを持っています。Eloquent also offers advanced subquery support, which allows you to pull information from related tables in a single query. For example, let's imagine that we have a table of flight destinations and a table of flights to destinations. The flights table contains an arrived_at column which indicates when the flight arrived at the destination.

selectaddSelectで利用できるサブクエリの機能を使えば全destinationsと、一番早く目的地へ到着するのフライト名を1回のクエリで取得できます。Using the subquery functionality available to the select and addSelect methods, we can select all of the destinations and the name of the flight that most recently arrived at that destination using a single query:

use App\Destination;
use App\Flight;

return Destination::addSelect(['last_flight' => Flight::select('name')
    ->whereColumn('destination_id', 'destinations.id')
    ->orderBy('arrived_at', 'desc')
    ->limit(1)
])->get();

サブクエリのオーダーSubquery Ordering

さらに、クエリビルダのorderBy関数もサブクエリをサポートしています。この機能を使い、ラストフライトが目的地へいつ到着するかに基づいて全目的地をソートしてみましょう。今回も、これによりデータベースに対し1回のクエリしか実行されません。In addition, the query builder's orderBy function supports subqueries. We may use this functionality to sort all destinations based on when the last flight arrived at that destination. Again, this may be done while executing a single query against the database:

return Destination::orderByDesc(
    Flight::select('arrived_at')
        ->whereColumn('destination_id', 'destinations.id')
        ->orderBy('arrived_at', 'desc')
        ->limit(1)
)->get();

1モデル/集計の取得Retrieving Single Models / Aggregates

指定したテーブルの全レコードを取得することに加え、findfirstfirstWhereを使い1レコードだけを取得できます。モデルのコレクションの代わりに、これらのメソッドは1モデルインスタンスを返します。In addition to retrieving all of the records for a given table, you may also retrieve single records using find, first, or firstWhere. Instead of returning a collection of models, these methods return a single model instance:

// 主キーで指定したモデル取得
$flight = App\Flight::find(1);

// クエリ条件にマッチした最初のモデル取得
$flight = App\Flight::where('active', 1)->first();

// クエリ条件にマッチした最初のモデル取得の短縮記法
$flight = App\Flight::firstWhere('active', 1);

また、主キーの配列をfindメソッドに渡し、呼び出すこともできます。一致したレコードのコレクションが返されます。You may also call the find method with an array of primary keys, which will return a collection of the matching records:

$flights = App\Flight::find([1, 2, 3]);

最初の結果が見つからない場合に、他のクエリやアクションの結果を取得したい場合もあると思います。firstOrメソッドは見つかった最初の結果を返すか、クエリ結果が見つからなかった場合にはコールバックを実行します。コールバックの結果はfirstOrメソッドの結果になります。Sometimes you may wish to retrieve the first result of a query or perform some other action if no results are found. The firstOr method will return the first result that is found or, if no results are found, execute the given callback. The result of the callback will be considered the result of the firstOr method:

$model = App\Flight::where('legs', '>', 100)->firstOr(function () {
        // ...
});

firstOrメソッドは、取得したいカラムの配列を引数に指定できます。The firstOr method also accepts an array of columns to retrieve:

$model = App\Flight::where('legs', '>', 100)
            ->firstOr(['id', 'legs'], function () {
                // ...
            });

Not Found例外Not Found Exceptions

モデルが見つからない時に、例外を投げたい場合もあります。これはとくにルートやコントローラの中で便利です。findOrFailメソッドとクエリの最初の結果を取得するfirstOrFailメソッドは、該当するレコードが見つからない場合にIlluminate\Database\Eloquent\ModelNotFoundException例外を投げます。Sometimes you may wish to throw an exception if a model is not found. This is particularly useful in routes or controllers. The findOrFail and firstOrFail methods will retrieve the first result of the query; however, if no result is found, a Illuminate\Database\Eloquent\ModelNotFoundException will be thrown:

$model = App\Flight::findOrFail(1);

$model = App\Flight::where('legs', '>', 100)->firstOrFail();

この例外がキャッチされないと自動的に404HTTPレスポンスがユーザーに送り返されます。これらのメソッドを使用すればわざわざ明確に404レスポンスを返すコードを書く必要はありません。If the exception is not caught, a 404 HTTP response is automatically sent back to the user. It is not necessary to write explicit checks to return 404 responses when using these methods:

Route::get('/api/flights/{id}', function ($id) {
    return App\Flight::findOrFail($id);
});

集計の取得Retrieving Aggregates

もちろんクエリビルダが提供しているcountsummaxや、その他の集計関数を使用することもできます。これらのメソッドは完全なモデルインスタンスではなく、最適なスカラー値を返します。You may also use the count, sum, max, and other aggregate methods[/docs/{{version}}/queries#aggregates] provided by the query builder[/docs/{{version}}/queries]. These methods return the appropriate scalar value instead of a full model instance:

$count = App\Flight::where('active', 1)->count();

$max = App\Flight::where('active', 1)->max('price');

モデルの追加と更新Inserting & Updating Models

InsertsInserts

モデルから新しいレコードを作成するには新しいインスタンスを作成し、saveメソッドを呼び出します。To create a new record in the database, create a new model instance, set attributes on the model, then call the save method:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Flight;
use Illuminate\Http\Request;

class FlightController extends Controller
{
    /**
     * 新しいflightインスタンスの生成
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        // リクエストのバリデート処理…

        $flight = new Flight;

        $flight->name = $request->name;

        $flight->save();
    }
}

この例では、受信したHTTPリクエストのnameパラメーターをApp\Flightモデルインスタンスのname属性に代入しています。saveメソッドが呼ばれると新しいレコードがデータベースに挿入されます。saveが呼び出された時にcreated_atupdated_atタイムスタンプは自動的に設定されますので、わざわざ設定する必要はありません。In this example, we assign the name parameter from the incoming HTTP request to the name attribute of the App\Flight model instance. When we call the save method, a record will be inserted into the database. The created_at and updated_at timestamps will automatically be set when the save method is called, so there is no need to set them manually.

UpdatesUpdates

saveメソッドはデータベースですでに存在するモデルを更新するためにも使用されます。モデルを更新するにはまず取得する必要があり、更新したい属性をセットしてそれからsaveメソッドを呼び出します。この場合もupdated_atタイムスタンプは自動的に更新されますので、値を指定する手間はかかりません。The save method may also be used to update models that already exist in the database. To update a model, you should retrieve it, set any attributes you wish to update, and then call the save method. Again, the updated_at timestamp will automatically be updated, so there is no need to manually set its value:

$flight = App\Flight::find(1);

$flight->name = 'New Flight Name';

$flight->save();

複数モデル更新Mass Updates

指定したクエリに一致する複数のモデルに対し更新することもできます。以下の例ではactiveで到着地(destination)がSan Diegoの全フライトに遅延(delayed)のマークを付けています。Updates can also be performed against any number of models that match a given query. In this example, all flights that are active and have a destination of San Diego will be marked as delayed:

App\Flight::where('active', 1)
          ->where('destination', 'San Diego')
          ->update(['delayed' => 1]);

updateメソッドは更新したいカラムと値の配列を受け取ります。The update method expects an array of column and value pairs representing the columns that should be updated.

Note: note Eloquentの複数モデル更新を行う場合、更新モデルに対するsavingsavedupdatingupdatedモデルイベントは発行されません。その理由は複数モデル更新を行う時、実際にモデルが取得されるわけではないからです。{note} When issuing a mass update via Eloquent, the saving, saved, updating, and updated model events will not be fired for the updated models. This is because the models are never actually retrieved when issuing a mass update.

属性変化の判定Examining Attribute Changes

モデル内部の状態が変化したかを判定し、ロード時のオリジナルな状態からどのように変化したかを調べるため、EloquentはisDirtyisCleanwasChangedメソッドを提供しています。Eloquent provides the isDirty, isClean, and wasChanged methods to examine the internal state of your model and determine how its attributes have changed from when they were originally loaded.

isDirtyメソッドはロードされたモデルから属性に変化があったかを判定します。特定の属性に変化があったかを調べるために、属性名を渡し指定できます。isCleanメソッドはisDirtyの反対の働きをし、同様に属性をオプショナルな引数として渡すことができます。The isDirty method determines if any attributes have been changed since the model was loaded. You may pass a specific attribute name to determine if a particular attribute is dirty. The isClean method is the opposite of isDirty and also accepts an optional attribute argument:

$user = User::create([
    'first_name' => 'Taylor',
    'last_name' => 'Otwell',
    'title' => 'Developer',
]);

$user->title = 'Painter';

$user->isDirty(); // true
$user->isDirty('title'); // true
$user->isDirty('first_name'); // false

$user->isClean(); // false
$user->isClean('title'); // false
$user->isClean('first_name'); // true

$user->save();

$user->isDirty(); // false
$user->isClean(); // true

wasChangedメソッドは現在のリクエストサイクル中、最後にモデルが保存されたときから属性に変化があったかを判定します。特定の属性が変化したかを調べるために、属性名を渡すことも可能です。The wasChanged method determines if any attributes were changed when the model was last saved within the current request cycle. You may also pass an attribute name to see if a particular attribute was changed:

$user = User::create([
    'first_name' => 'Taylor',
    'last_name' => 'Otwell',
    'title' => 'Developer',
]);

$user->title = 'Painter';
$user->save();

$user->wasChanged(); // true
$user->wasChanged('title'); // true
$user->wasChanged('first_name'); // false

getOriginalメソッドはモデルのロード後にどんな変更がされているかにかかわらず、モデルのオリジナルな属性を含む配列を返します。特定の属性のオリジナル値を取得するために属性名を指定できます。The getOriginal method returns an array containing the original attributes of the model regardless of any changes since the model was loaded. You may pass a specific attribute name to get the original value of a particular attribute:

$user = User::find(1);

$user->name; // John
$user->email; // john@example.com

$user->name = "Jack";
$user->name; // Jack

$user->getOriginal('name'); // John
$user->getOriginal(); // オリジナルな属性の配列

複数代入Mass Assignment

一行だけで新しいモデルを保存するには、createメソッドが利用できます。挿入されたモデルインスタンスが、メソッドから返されます。しかし、これを利用する前に、Eloquentモデルはデフォルトで複数代入から保護されているため、モデルへfillableguarded属性のどちらかを設定する必要があります。You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment by default.

複数代入の脆弱性はリクエストを通じて予期しないHTTPパラメーターが送られた時に起き、そのパラメーターはデータベースのカラムを予期しないように変更してしまうでしょう。たとえば悪意のあるユーザーがHTTPパラメーターでis_adminパラメーターを送り、それがモデルのcreateメソッドに対して渡されると、そのユーザーは自分自身を管理者(administrator)に昇格できるのです。A mass-assignment vulnerability occurs when a user passes an unexpected HTTP parameter through a request, and that parameter changes a column in your database you did not expect. For example, a malicious user might send an is_admin parameter through an HTTP request, which is then passed into your model's create method, allowing the user to escalate themselves to an administrator.

ですから最初に複数代入したいモデルの属性を指定してください。モデルの$fillableプロパティで指定できます。たとえば、Flightモデルの複数代入でname属性のみ使いたい場合です。So, to get started, you should define which model attributes you want to make mass assignable. You may do this using the $fillable property on the model. For example, let's make the name attribute of our Flight model mass assignable:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * 複数代入する属性
     *
     * @var array
     */
    protected $fillable = ['name'];
}

複数代入する属性を指定したら、新しいレコードをデータベースに挿入するため、createが利用できます。createメソッドは保存したモデルインスタンスを返します。Once we have made the attributes mass assignable, we can use the create method to insert a new record in the database. The create method returns the saved model instance:

$flight = App\Flight::create(['name' => 'Flight 10']);

すでに存在するモデルインスタンスへ属性を指定したい場合は、fillメソッドを使い、配列で指定してください。If you already have a model instance, you may use the fill method to populate it with an array of attributes:

$flight->fill(['name' => 'Flight 22']);

複数代入の許可Allowing Mass Assignment

全属性を複数代入可能にする場合は、$guardedプロパティに空の配列を定義します。If you would like to make all attributes mass assignable, you may define the $guarded property as an empty array:

/**
 * 複数代入しない属性
 *
 * @var array
 */
protected $guarded = [];

他の生成メソッドOther Creation Methods

firstOrCreate/ firstOrNewfirstOrCreate/ firstOrNew

他にも属性の複数代入可能な生成メソッドが2つあります。firstOrCreatefirstOrNewです。firstOrCreateメソッドは指定されたカラム/値ペアでデータベースレコードを見つけようします。モデルがデータベースで見つからない場合は、最初の引数が表す属性、任意の第2引数があればそれが表す属性も同時に含む、レコードが挿入されます。There are two other methods you may use to create models by mass assigning attributes: firstOrCreate and firstOrNew. The firstOrCreate method will attempt to locate a database record using the given column / value pairs. If the model can not be found in the database, a record will be inserted with the attributes from the first parameter, along with those in the optional second parameter.

firstOrNewメソッドもfirstOrCreateのように指定された属性にマッチするデータベースのレコードを見つけようとします。しかしモデルが見つからない場合、新しいモデルインスタンスが返されます。firstOrNewが返すモデルはデータベースに保存されていないことに注目です。保存するにはsaveメソッドを呼び出す必要があります。The firstOrNew method, like firstOrCreate will attempt to locate a record in the database matching the given attributes. However, if a model is not found, a new model instance will be returned. Note that the model returned by firstOrNew has not yet been persisted to the database. You will need to call save manually to persist it:

// nameでフライトを取得するか、存在しなければ作成する
$flight = App\Flight::firstOrCreate(['name' => 'Flight 10']);

// nameでフライトを取得するか、存在しなければ指定されたname、delayed、arrival_timeを含め、インスタンス化する
$flight = App\Flight::firstOrCreate(
    ['name' => 'Flight 10'],
    ['delayed' => 1, 'arrival_time' => '11:30']
);

// nameで取得するか、インスタンス化する
$flight = App\Flight::firstOrNew(['name' => 'Flight 10']);

// nameで取得するか、name、delayed、arrival_time属性でインスタンス化する
$flight = App\Flight::firstOrNew(
    ['name' => 'Flight 10'],
    ['delayed' => 1, 'arrival_time' => '11:30']
);

updateOrCreateupdateOrCreate

また、既存のモデルを更新するか、存在しない場合は新しいモデルを作成したい状況も存在します。これを一度に行うため、LaravelではupdateOrCreateメソッドを提供しています。firstOrCreateメソッドと同様に、updateOrCreateもモデルを保存するため、save()を呼び出す必要はありません。You may also come across situations where you want to update an existing model or create a new model if none exists. Laravel provides an updateOrCreate method to do this in one step. Like the firstOrCreate method, updateOrCreate persists the model, so there's no need to call save():

// OaklandからSan Diego行きの飛行機があれば、料金へ99ドルを設定する。
// 一致するモデルがなければ、作成する。
$flight = App\Flight::updateOrCreate(
    ['departure' => 'Oakland', 'destination' => 'San Diego'],
    ['price' => 99, 'discounted' => 1]
);

モデル削除Deleting Models

モデルを削除するには、モデルに対しdeleteメソッドを呼び出します。To delete a model, call the delete method on a model instance:

$flight = App\Flight::find(1);

$flight->delete();

キーによる既存モデルの削除Deleting An Existing Model By Key

上記の例ではdeleteメソッドを呼び出す前に、データベースからモデルを取得しています。しかしモデルの主キーが分かっている場合は、明示的にモデルを取得せずともdestroyメソッドで削除できます。さらに、引数に主キーを一つ指定できるだけでなく、destroyメソッドは主キーの配列や、主キーのコレクションを引数に指定することで、複数のキーを指定できます。In the example above, we are retrieving the model from the database before calling the delete method. However, if you know the primary key of the model, you may delete the model without explicitly retrieving it by calling the destroy method. In addition to a single primary key as its argument, the destroy method will accept multiple primary keys, an array of primary keys, or a collection[/docs/{{version}}/collections] of primary keys:

App\Flight::destroy(1);

App\Flight::destroy(1, 2, 3);

App\Flight::destroy([1, 2, 3]);

App\Flight::destroy(collect([1, 2, 3]));

Note: note destroyメソッドは個別にモデルをロードし、deleteメソッドを呼び出します。そのため、deletingdeletedイベントが発行されます。{note} The destroy method loads each model individually and calls the delete method on them so that the deleting and deleted events are fired.

クエリによるモデル削除Deleting Models By Query

一連のモデルに対する削除文を実行することもできます。次の例はactiveではない印を付けられたフライトを削除しています。複数モデル更新と同様に、複数削除は削除されるモデルに対するモデルイベントを発行しません。You can also run a delete statement on a set of models. In this example, we will delete all flights that are marked as inactive. Like mass updates, mass deletes will not fire any model events for the models that are deleted:

$deletedRows = App\Flight::where('active', 0)->delete();

Note: note 複数削除文をEloquentにより実行する時、削除対象モデルに対するdeletingdeletedモデルイベントは発行されません。なぜなら、削除文の実行時に、実際にそのモデルが取得されるわけではないためです。{note} When executing a mass delete statement via Eloquent, the deleting and deleted model events will not be fired for the deleted models. This is because the models are never actually retrieved when executing the delete statement.

ソフトデリートSoft Deleting

本当にデータベースからレコードを削除する方法に加え、Eloquentはモデルの「ソフトデリート」も行えます。モデルがソフトデリートされても実際にはデータベースのレコードから削除されません。代わりにそのモデルへdeleted_at属性がセットされ、データベースへ書き戻されます。モデルのdeleted_atの値がNULLでない場合、ソフトデリートされています。モデルのソフトデリートを有効にするには、モデルにIlluminate\Database\Eloquent\SoftDeletesトレイトを使います。In addition to actually removing records from your database, Eloquent can also "soft delete" models. When models are soft deleted, they are not actually removed from your database. Instead, a deleted_at attribute is set on the model and inserted into the database. If a model has a non-null deleted_at value, the model has been soft deleted. To enable soft deletes for a model, use the Illuminate\Database\Eloquent\SoftDeletes trait on the model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Flight extends Model
{
    use SoftDeletes;
}

lightbulb">Tip!! SoftDeletesトレイトは自動的にdeleted_at属性をDateTime/Carbonインスタンスへ変換します。{tip} The SoftDeletes trait will automatically cast the deleted_at attribute to a DateTime / Carbon instance for you.

データベーステーブルにもdeleted_atカラムを追加する必要があります。Laravelスキーマビルダにはこのカラムを作成するメソッドが存在しています。You should also add the deleted_at column to your database table. The Laravel schema builder[/docs/{{version}}/migrations] contains a helper method to create this column:

public function up()
{
    Schema::table('flights', function (Blueprint $table) {
        $table->softDeletes();
    });
}

public function down()
{
    Schema::table('flights', function (Blueprint $table) {
        $table->dropSoftDeletes();
    });
}

これでモデルに対しdeleteメソッドを使用すれば、deleted_atカラムに現在の時間がセットされます。ソフトデリートされたモデルに対しクエリがあっても、削除済みのモデルはクエリ結果に含まれません。Now, when you call the delete method on the model, the deleted_at column will be set to the current date and time. And, when querying a model that uses soft deletes, the soft deleted models will automatically be excluded from all query results.

指定されたモデルインスタンスがソフトデリートされているかを確認するには、trashedメソッドを使います。To determine if a given model instance has been soft deleted, use the trashed method:

if ($flight->trashed()) {
    //
}

ソフトデリート済みモデルのクエリQuerying Soft Deleted Models

ソフトデリート済みモデルも含めるIncluding Soft Deleted Models

前述のようにソフトデリートされたモデルは自動的にクエリの結果から除外されます。しかし結果にソフトデリート済みのモデルを含めるように強制したい場合は、クエリにwithTrashedメソッドを使ってください。As noted above, soft deleted models will automatically be excluded from query results. However, you may force soft deleted models to appear in a result set using the withTrashed method on the query:

$flights = App\Flight::withTrashed()
                ->where('account_id', 1)
                ->get();

withTrashedメソッドはリレーションのクエリにも使えます。The withTrashed method may also be used on a relationship[/docs/{{version}}/eloquent-relationships] query:

$flight->history()->withTrashed()->get();

ソフトデリート済みモデルのみの取得Retrieving Only Soft Deleted Models

onlyTrashedメソッドによりソフトデリート済みのモデルのみを取得できます。The onlyTrashed method will retrieve only soft deleted models:

$flights = App\Flight::onlyTrashed()
                ->where('airline_id', 1)
                ->get();

ソフトデリートの解除Restoring Soft Deleted Models

時にはソフトデリート済みのモデルを「未削除」に戻したい場合も起きます。ソフトデリート済みモデルを有効な状態に戻すには、そのモデルインスタンスに対しrestoreメソッドを使ってください。Sometimes you may wish to "un-delete" a soft deleted model. To restore a soft deleted model into an active state, use the restore method on a model instance:

$flight->restore();

複数のモデルを手っ取り早く未削除に戻すため、クエリにrestoreメソッドを使うこともできます。他の「複数モデル」操作と同様に、この場合も復元されるモデルに対するモデルイベントは、発行されません。You may also use the restore method in a query to quickly restore multiple models. Again, like other "mass" operations, this will not fire any model events for the models that are restored:

App\Flight::withTrashed()
        ->where('airline_id', 1)
        ->restore();

withTrashedメソッドと同様、restoreメソッドはリレーションに対しても使用できます。Like the withTrashed method, the restore method may also be used on relationships[/docs/{{version}}/eloquent-relationships]:

$flight->history()->restore();

モデルの完全削除Permanently Deleting Models

データベースからモデルを本当に削除する場合もあるでしょう。データベースからソフトデリート済みモデルを永久に削除するにはforceDeleteメソッドを使います。Sometimes you may need to truly remove a model from your database. To permanently remove a soft deleted model from the database, use the forceDelete method:

// 1モデルを完全に削除する
$flight->forceDelete();

// 関係するモデルを全部完全に削除する
$flight->history()->forceDelete();

複製モデルReplicating Models

replicateメソッドを用いて、あるモデルインスタンスの未保存なコピーを作成できます。これは共通の同じ属性をたくさん持つモデルインスタンスを作成したい場合にとくに便利です。You may create an unsaved copy of a model instance using the replicate method. This is particularly useful when you have model instances that share many of the same attributes:

$shipping = App\Address::create([
    'type' => 'shipping',
    'line_1' => '123 Example Street',
    'city' => 'Victorville',
    'state' => 'CA',
    'postcode' => '90001',
]);

$billing = $shipping->replicate()->fill([
    'type' => 'billing'
]);

$billing->save();

クエリスコープQuery Scopes

グローバルスコープGlobal Scopes

グローバルスコープにより、指定したモデルのクエリに対して、制約を付け加えることができます。Laravel自身のソフトデリート機能は、「削除されていない」モデルをデータベースから取得するためにグローバルスコープを使用しています。独自のグローバルスコープを書くことにより、特定のモデルのクエリに制約を確実に、簡単に、便利に指定できます。Global scopes allow you to add constraints to all queries for a given model. Laravel's own soft delete[#soft-deleting] functionality utilizes global scopes to only pull "non-deleted" models from the database. Writing your own global scopes can provide a convenient, easy way to make sure every query for a given model receives certain constraints.

グローバルスコープの記述Writing Global Scopes

グローバルスコープは簡単に書けます。Illuminate\Database\Eloquent\Scopeインターフェイスを実装したクラスを定義します。このインターフェイスは、applyメソッドだけを実装するように要求しています。applyメソッドは必要に応じ、where制約を追加します。Writing a global scope is simple. Define a class that implements the Illuminate\Database\Eloquent\Scope interface. This interface requires you to implement one method: apply. The apply method may add where constraints to the query as needed:

<?php

namespace App\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class AgeScope implements Scope
{
    /**
     * Eloquentクエリビルダへ適用するスコープ
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        $builder->where('age', '>', 200);
    }
}

lightbulb">Tip!! クエリのSELECT節にカラムを追加するグローバルスコープの場合は、selectの代わりにaddSelectメソッドを使用してください。これにより、クエリの存在するSELECT節を意図せずに置き換えてしまうのを防げます。{tip} If your global scope is adding columns to the select clause of the query, you should use the addSelect method instead of select. This will prevent the unintentional replacement of the query's existing select clause.

グローバルスコープの適用Applying Global Scopes

モデルにグローバルスコープを適用するには、そのモデルのbootedメソッドをオーバライドし、addGlobalScopeメソッドを呼び出します。To assign a global scope to a model, you should override a given model's booted method and use the addGlobalScope method:

<?php

namespace App;

use App\Scopes\AgeScope;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * モデルの「初期起動」メソッド
     *
     * @return void
     */
    protected static function booted()
    {
        static::addGlobalScope(new AgeScope);
    }
}

スコープを追加した後から、User::all()は以下のクエリを生成するようになります。After adding the scope, a query to User::all() will produce the following SQL:

select * from `users` where `age` > 200

クロージャによるグローバルスコープAnonymous Global Scopes

Eloquentではクロージャを使ったグローバルスコープも定義できます。独立したクラスを使うだけの理由がない、簡単なスコープを使いたい場合、とくに便利です。Eloquent also allows you to define global scopes using Closures, which is particularly useful for simple scopes that do not warrant a separate class:

<?php

namespace App;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * モデルの「初期起動」メソッド
     *
     * @return void
     */
    protected static function booted()
    {
        static::addGlobalScope('age', function (Builder $builder) {
            $builder->where('age', '>', 200);
        });
    }
}

グローバルスコープの削除Removing Global Scopes

特定のクエリからグローバルスコープを削除した場合は、withoutGlobalScopeメソッドを使います。唯一の引数として、クラス名を受けます。If you would like to remove a global scope for a given query, you may use the withoutGlobalScope method. The method accepts the class name of the global scope as its only argument:

User::withoutGlobalScope(AgeScope::class)->get();

もしくは、クロージャを使用し、グローバルスコープを定義している場合は:Or, if you defined the global scope using a Closure:

User::withoutGlobalScope('age')->get();

複数、もしくは全部のグローバルスコープを削除したい場合も、withoutGlobalScopesメソッドが使えます。If you would like to remove several or even all of the global scopes, you may use the withoutGlobalScopes method:

// 全グローバルスコープの削除
User::withoutGlobalScopes()->get();

// いくつかのグローバルスコープの削除
User::withoutGlobalScopes([
    FirstScope::class, SecondScope::class
])->get();

ローカルスコープLocal Scopes

ローカルスコープによりアプリケーション全体で簡単に再利用可能な、一連の共通制約を定義できます。たとえば、人気のある(popular)ユーザーを全員取得する必要が、しばしばあるとしましょう。スコープを定義するには、scopeを先頭につけた、Eloquentモデルのメソッドを定義します。Local scopes allow you to define common sets of constraints that you may easily re-use throughout your application. For example, you may need to frequently retrieve all users that are considered "popular". To define a scope, prefix an Eloquent model method with scope.

スコープはいつもクエリビルダインスタンスを返します。Scopes should always return a query builder instance:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * 人気のあるユーザーだけに限定するクエリスコープ
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopePopular($query)
    {
        return $query->where('votes', '>', 100);
    }

    /**
     * アクティブなユーザーだけに限定するクエリスコープ
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeActive($query)
    {
        return $query->where('active', 1);
    }
}

ローカルスコープの利用Utilizing A Local Scope

スコープが定義できたらモデルのクエリ時にスコープメソッドを呼び出せます。しかし、メソッドを呼び出すときはscopeプレフィックスをつけないでください。さまざまなスコープをチェーンでつなぎ呼び出すこともできます。例を見てください。Once the scope has been defined, you may call the scope methods when querying the model. However, you should not include the scope prefix when calling the method. You can even chain calls to various scopes, for example:

$users = App\User::popular()->active()->orderBy('created_at')->get();

orクエリ操作により、複数のEloquentモデルスコープを組み合わせるには、クロージャのコールバックを使用する必要があります。Combining multiple Eloquent model scopes via an or query operator may require the use of Closure callbacks:

$users = App\User::popular()->orWhere(function (Builder $query) {
    $query->active();
})->get();

しかし、上記は手間がかかるため、Laravelはクロージャを使用せずにスコープをスラスラとチェーンできるように、"higher order" orWhereメソッドを用意しています。However, since this can be cumbersome, Laravel provides a "higher order" orWhere method that allows you to fluently chain these scopes together without the use of Closures:

$users = App\User::popular()->orWhere->active()->get();

動的スコープDynamic Scopes

引数を受け取るスコープを定義したい場合もあるでしょう。スコープにパラメーターを付けるだけです。スコープパラメーターは$query引数の後に定義しする必要があります。Sometimes you may wish to define a scope that accepts parameters. To get started, just add your additional parameters to your scope. Scope parameters should be defined after the $query parameter:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * 指定したタイプのユーザーだけを含むクエリのスコープ
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  mixed  $type
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeOfType($query, $type)
    {
        return $query->where('type', $type);
    }
}

これでスコープを呼び出すときにパラメーターを渡せます。Now, you may pass the parameters when calling the scope:

$users = App\User::ofType('admin')->get();

モデルの比較Comparing Models

時に2つのモデルが「同じ」であるかを判定する必要が起きるでしょう。isメソッドは2つのモデルが、同じ主キー、テーブル、データベース接続を持っているかを確認します。Sometimes you may need to determine if two models are the "same". The is method may be used to quickly verify two models have same primary key, table, and database connection:

if ($post->is($anotherPost)) {
    //
}

イベントEvents

Eloquentモデルは多くのイベントを発行します。creatingcreatedupdatingupdatedsavingsaveddeletingdeletedrestoringrestoredretrievedのメソッドを利用し、モデルのライフサイクルのさまざまな時点をフックできます。イベントにより特定のモデルクラスが保存されたり、アップデートされたりするたびに簡単にコードを実行できるようになります。各イベントは、コンストラクタによりモデルのインスタンスを受け取ります。Eloquent models fire several events, allowing you to hook into the following points in a model's lifecycle: retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored. Events allow you to easily execute code each time a specific model class is saved or updated in the database. Each event receives the instance of the model through its constructor.

データベースから既存のモデルを取得した時にretrievedイベントは発行されます。新しいアイテムが最初に保存される場合、creatingcreatedイベントが発行されます。既存モデルを更新しsaveメソッドを呼び出すと、updatingupdatedイベントが発行されます。モデルが生成・更新される時はsavingsavedイベントが発行されます。The retrieved event will fire when an existing model is retrieved from the database. When a new model is saved for the first time, the creating and created events will fire. The updating / updated events will fire when an existing model is modified and the save method is called. The saving / saved events will fire when a model is created or updated.

Note: note Eloquentの複数モデル更新・削除を行う場合、影響を受けるモデルに対するsavedupdateddeletingモデルイベントは発行されません。その理由は複数モデル更新・削除を行う時、実際にモデルが取得されるわけではないからです。{note} When issuing a mass update or delete via Eloquent, the saved, updated, deleting, and deleted model events will not be fired for the affected models. This is because the models are never actually retrieved when issuing a mass update or delete.

使用するには、Eloquentモデルに$dispatchesEventsプロパティを定義します。これにより、Eloquentモデルのライフサイクルのさまざまな時点を皆さん自身のイベントクラスへマップします。To get started, define a $dispatchesEvents property on your Eloquent model that maps various points of the Eloquent model's lifecycle to your own event classes[/docs/{{version}}/events]:

<?php

namespace App;

use App\Events\UserDeleted;
use App\Events\UserSaved;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * モデルのイベントマップ
     *
     * @var array
     */
    protected $dispatchesEvents = [
        'saved' => UserSaved::class,
        'deleted' => UserDeleted::class,
    ];
}

Eloquentイベントの定義とマップができたら、イベントリスナを使用し、イベントを処理できます。After defining and mapping your Eloquent events, you may use event listeners[https://laravel.com/docs/{{version}}/events#defining-listeners] to handle the events.

クロージャの使用Using Closures

カスタムイベントクラスを使用する代わりに、さまざまなモデルイベントが発行されたときに実行されるクロージャを登録できます。通常はモデルのbootedメソッドで、これらのクロージャを登録すべきでしょう。Instead of using custom event classes, you may register Closures that execute when various model events are fired. Typically, you should register these Closures in the booted method of your model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * モデルの「初期起動」メソッド
     *
     * @return void
     */
    protected static function booted()
    {
        static::created(function ($user) {
            //
        });
    }
}

オブザーバObservers

オブザーバの定義Defining Observers

特定のモデルに対し、多くのイベントをリスニングしている場合、全リスナのグループに対するオブザーバを一つのクラスの中で使用できます。オブザーバクラスは、リッスンしたいEloquentイベントに対応する名前のメソッドを持ちます。これらのメソッドは、唯一の引数としてモデルを受け取ります。make:observer Artisanコマンドで、新しいオブザーバクラスを簡単に生成できます。If you are listening for many events on a given model, you may use observers to group all of your listeners into a single class. Observers classes have method names which reflect the Eloquent events you wish to listen for. Each of these methods receives the model as their only argument. The make:observer Artisan command is the easiest way to create a new observer class:

php artisan make:observer UserObserver --model=User

このコマンドは、App/Observersディレクトリへ新しいオブザーバを設置します。このディレクトリが存在しなければ、Artisanが作成します。真新しいオブザーバは、次の通りです。This command will place the new observer in your App/Observers directory. If this directory does not exist, Artisan will create it for you. Your fresh observer will look like the following:

<?php

namespace App\Observers;

use App\User;

class UserObserver
{
    /**
     * Userの"created"イベントを処理
     *
     * @param  \App\User  $user
     * @return void
     */
    public function created(User $user)
    {
        //
    }

    /**
     * Userの"updated"イベントを処理
     *
     * @param  \App\User  $user
     * @return void
     */
    public function updated(User $user)
    {
        //
    }

    /**
     * Userの"deleted"イベントを処理
     *
     * @param  \App\User  $user
     * @return void
     */
    public function deleted(User $user)
    {
        //
    }

    /**
     * Userの"forceDeleted"イベントを処理
     *
     * @param  \App\User  $user
     * @return void
     */
    public function forceDeleted(User $user)
    {
        //
    }
}

オブザーバを登録するには、監視したいモデルに対し、observeメソッドを使用します。サービスプロバイダの一つの、bootメソッドで登録します。以下の例では、AppServiceProviderでオブザーバを登録しています。To register an observer, use the observe method on the model you wish to observe. You may register observers in the boot method of one of your service providers. In this example, we'll register the observer in the AppServiceProvider:

<?php

namespace App\Providers;

use App\Observers\UserObserver;
use App\User;
use Illuminate\Support\ServiceProvider;

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

    /**
     * 全アプリケーションサービスの初期起動
     *
     * @return void
     */
    public function boot()
    {
        User::observe(UserObserver::class);
    }
}

イベントのミュートMuting Events

モデルが発行させたすべてのイベントを一時的に「ミュート」したい場合があります。withoutEventsメソッドで可能です。withoutEventsメソッドは引数として唯一クロージャを受け付けます。クロージャ内で実行するコードはモデルイベントを発行しません。たとえば、次の例はモデルイベントを発生せずにApp\Userインスタンスを取得し削除します。指定したクロージャが返す値はすべて、withoutEventsメソッドがそのまま返します。You may occasionally wish to temporarily "mute" all events fired by a model. You may achieve this using the withoutEvents method. The withoutEvents method accepts a Closure as its only argument. Any code executed within this Closure will not fire model events. For example, the following will fetch and delete an App\User instance without firing any model events. Any value returned by the given Closure will be returned by the withoutEvents method:

use App\User;

$user = User::withoutEvents(function () use () {
    User::findOrFail(1)->delete();

    return User::find(2);
});

章選択

設定

明暗テーマ
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に保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作