Too many fillable fields in model Laravel?
Solution 1
If you need to set all columns as fillable, do this in the model:
protected $guarded = [];
If you would like to make all attributes mass assignable, you may define the $guarded property as an empty array
https://laravel.com/docs/5.3/eloquent#mass-assignment
Solution 2
In such scenario, you can try doing the reverse. For example: id
, created_at
and updated_at
field as $guarded. Like:
protected $guarded = ['id', 'created_at', 'updated_at'];
Except these rest will be considered as fillable
i.e. mass assignable.
You can find details in Official Laravel Doc
Guarding Attributes
While $fillable serves as a "white list" of attributes that should be mass assignable, you may also choose to use $guarded. The $guarded property should contain an array of attributes that you do not want to be mass assignable. All other attributes not in the array will be mass assignable. So, $guarded functions like a "black list". Of course, you should use either $fillable or $guarded - not both.
Related videos on Youtube
Griboedov
Updated on July 09, 2022Comments
-
Griboedov almost 2 years
I have around 200 fields in a table that are numbered:
field_1 field_2 etc
I tried to insert data in table:
Result::insert($data);
Where
$data
is multiple array:$data = [] = array("field_1" => 3); $data = [] = array("field_1" => 2);
Can I set
*
in optionprotected $fillable = ["*"];
to make all fields fillable?-
Kyslik over 7 yearsCode smells all over the place. (database smells)
-
-
Griboedov over 7 yearsWhere
id
is primary key (Autoincrement)? -
Mahfuzul Alam over 7 yearsYes if you are using
$table->increments('id')
in your migration file.