Laravel Eloquent ignore attribute if not in table during insert

前端 未结 4 1458
不思量自难忘°
不思量自难忘° 2021-02-19 03:44

I have a model Foo that corresponds to a table with the following columns.

id
description
user_id

I\'m setting the attributes of the Foo model individual

4条回答
  •  醉话见心
    2021-02-19 04:31

    Add the $fillable and the model will ignore everything not in it (instead of giving an error). Using the constructor function to fill all the columns is optional.

    class Foo extends Model
    {
        protected $fillable = ['id', 'description', 'user_id'];
    }
    
    $f = new Foo(['id' => 1, 'description' => "hello monkey", 'user_id' => 55, 'bar' => 'wow']); // should work w/o error, but it ignores the 'bar'.
    

提交回复
热议问题