LoginSignup
16
14

More than 5 years have passed since last update.

laravelでfillでセーブしようとしたらエラーが出たとき

Posted at

初心者から職人になりたくて、fill使ったらエラーが起きて、エラー文でぐぐってもよくわからず、fillメソッド軽くソース読んだら$fillableに、保存したいcolumn名を入れないといけないみたい。

と思ったところでドキュメント読んだらちゃんと書いてあった。
まず、ドキュメントを読んで、わからなければソースを見るのがいいみたい。

// error
Add [_token] to fillable property to allow mass assignment on 

// このエラーとのときは、fillableを指定する
protected $fillable = ['hoge', 'fuga'];

// error ?? このときは、columnに足りないのをつけたす。
SQLSTATE[HY000]: General error: 1364 Field

// add
protected $fillable = ['hoge', 'fuga', 'piyo'];


<?php

namespace Illuminate\Database\Eloquent\Concerns;

use Illuminate\Support\Str;

trait GuardsAttributes
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

参考
LaravelのORMで初心者から職人へ
https://qiita.com/henriquebremenkanp/items/cd13944b0281297217a9

laravel/framework
framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php
https://github.com/laravel/framework/blob/5.7/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php

Laravel 5.5 Eloquent:利用の開始
複数代入
https://readouble.com/laravel/5.5/ja/eloquent.html#mass-assignment

16
14
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
16
14