search
LoginSignup
0

More than 1 year has passed since last update.

posted at

Organization

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

目的

  • 各モデルクラスでモデルに配列を追加するときに使用するfill()メソッドの記載場所をメモ的にまとめておく

場所

  • アプリ名ディレクトリ/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.phpの中に記載されている。
  • 下記に定義部分を抜粋して記載する。

    アプリ名ディレクトリ/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
    /**
     * Fill the model with an array of attributes.
     *
     * @param  array  $attributes
     * @return $this
     *
     * @throws \Illuminate\Database\Eloquent\MassAssignmentException
     */
    public function fill(array $attributes)
    {
        $totallyGuarded = $this->totallyGuarded();
    
        foreach ($this->fillableFromArray($attributes) as $key => $value) {
            $key = $this->removeTableFromKey($key);
    
            // The developers may choose to place some attributes in the "fillable" array
            // which means only those attributes may be set through mass assignment to
            // the model, and all others will just get ignored for security reasons.
            if ($this->isFillable($key)) {
                $this->setAttribute($key, $value);
            } elseif ($totallyGuarded) {
                throw new MassAssignmentException(sprintf(
                    'Add [%s] to fillable property to allow mass assignment on [%s].',
                    $key, get_class($this)
                ));
            }
        }
    
        return $this;
    }
    

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
What you can do with signing up
0