I want to update or create a model. It's not working at all.
Here is my code:
foreach ($ProfileRequest as $fieldname => $value){
ProfileRequest::updateOrCreate(['sm_id' => $uid, 'field_name' => $fieldname],
[
'sm_id' => $uid,
'field_name' => $fieldname,
'value' => $value
]);
}
This is working for insertion:
foreach ($ProfileRequest as $fieldname => $value) {
$req = new ProfileRequest;
$req->chef_id = $uid;
$req->field_name = $fieldname;
$req->value = $value;
$req->save();
}
You need to specify the fillable fields in your model since you are dealing with Mass Updates. Put this at your model
protected $fillable = ['field_name','value'];
Change your code like this
foreach ($ProfileRequest as $fieldname => $value){
ProfileRequest::updateOrCreate(
[
'sm_id' => $uid
],
[
'field_name' => $fieldname,
'value' => $value
]
);
}
Avoid repeating the fields. If somehow, my code doesn't work for you, please go check the function definition for yourself here: https://laravel.com/api/5.4/Illuminate/Database/Eloquent/Builder.html#method_updateOrCreate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With