Fillable fields in a Laravel model?

I have about 200 fields in a table that are numbered:

field_1 field_2 etc 

I tried to insert data into a table:

 Result::insert($data); 

Where $data are multiple arrays:

 $data = [] = array("field_1" => 3); $data = [] = array("field_1" => 2); 

Is it possible to set * the options protected $fillable = ["*"]; so that all fields are filled?

+8
eloquent laravel laravel-5
source share
3 answers

If you need to set all columns as fillable, do this in the model:

 protected $guarded = []; 

If you want all attributes to be assigned by masses, you can define the $ guarded property as an empty array

https://laravel.com/docs/5.3/eloquent#mass-assignment

+9
source share

In such a scenario, you can try to do the opposite. For example: id , created_at and updated_at field as $ protected. How:

 protected $guarded = ['id', 'created_at', 'updated_at']; 

Except for these others, fillable i.e. assigned mass .

You can find more details in the Laravel White Paper

Security Attributes

While $ fillable is used as a whitelist of attributes you should also use $ protected . The protected $ property must contain an array of attributes that you do not want to be assigned as mass. All other attributes not included in the array will be massive reassignment So $ protected works like black <black> Of course you must use either $ fillable or $ protected - not both.

+9
source share

Example Consider a payment as a model. You can do it

 Payment::unguard(); Payment::create($data); Payment::reguard(); 
0
source share

All Articles