目的
- 各モデルクラスでインスタンスを作るときに使用する
newInstance()
メソッドの記載場所をメモ的にまとめておく
場所
-
アプリ名ディレクトリ/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
の中に記載されている。 -
下記に定義部分を抜粋して記載する。
アプリ名ディレクトリ/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php/** * Create a new instance of the given model. * * @param array $attributes * @param bool $exists * @return static */ public function newInstance($attributes = [], $exists = false) { // This method just provides a convenient way for us to generate fresh model // instances of this current model. It is particularly useful during the // hydration of new objects via the Eloquent query builder instances. $model = new static((array) $attributes); $model->exists = $exists; $model->setConnection( $this->getConnectionName() ); $model->setTable($this->getTable()); return $model; }
各モデルクラスは
アプリ名ディレクトリ/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
に記載されているModelクラスを継承して作成されている。そのため各モデルクラスでもnewInstance()
メソッドを使うことができる。