I have a weird issue when updating eloquent model, I use a checkbox (1 || undefined ) and patch all the data after validation using
$input = $request->all();
$service->update($input);
I tried to check the checkbox specifically
$input['active'] = ($request->has('active') && $input['active']) ? 1 : 0
but it still wouldn't affect the database.
When I dump the Request
I can see active:
1
or 0
but nothing of that changes the database on update()
I did a quick testing and using
$service->active = ($request->has('active') && $input['active']) ? 1 : 0 ;
$service->save();
did the job. But why the update()
isn't updating this field ?
If $request->active
returns true, but it still doesn't save in DB, I bet you forgot to add active
to a $fillable
array:
protected $fillable = ['something', 'something_else', 'active'];
https://laravel.com/docs/5.3/eloquent#mass-assignment
Have you tried :
$input = $input->except(['active']);
$input['active'] = ($request->has('active') && $input['active']) ? 1 : 0;
$service->update($input);
Hope this helps.
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