LoginSignup
2
0

More than 1 year has passed since last update.

laravel newInstance() メソッドの定義場所 個人メモ

Posted at

目的

  • 各モデルクラスでインスタンスを作るときに使用する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()メソッドを使うことができる。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0