The purpose of $fillable is exactly that, not allowing mass assignment!
Also, instead of $fillable, you can use $guarded , and you will add only the fields that are NOT fillable, might be easier maybe.
Going to your point, right now i'm out of my environment, so i can't test what i'm saying, but try and give feedback
$model = new Model;
$model->fill( $array ); // or $model->fill( ['field' => 'attribute', 'field2' => 'attribute2'] );
$model->field3 = 'attribute3';
$model->save();
the fill method will be protected against mass assignment, and when you set the attribute won't
Cheers
Thanks. Yes, I understand the purpose of $fillable, but you should be able to assign fields TO a mass assignment I guess is what I am saying.
Thanks for your help, I will give it a shot.
Hmmm if i got you right now, with what you want, does this give any help?
$vars = $request->all();
$vars_to_add = ['attribute3' => 'value3'];
$vars = array_merge($vars, $vars_to_add);
Model::create($vars);
Again, i'm just typing, cannot test, but you get the point
Cheers
Thanks but that would fall into a mass assignment and nonfillables would be ignored. Your previous method did work, I was just hoping for something more graceful. :)
Thanks again!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community