Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel eloquent updating tinyint (boolean)

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 ?

like image 200
dev Avatar asked Nov 28 '22 00:11

dev


2 Answers

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

like image 99
Alexey Mezenin Avatar answered Dec 04 '22 02:12

Alexey Mezenin


Have you tried :

$input = $input->except(['active']);

$input['active'] = ($request->has('active') && $input['active']) ? 1 : 0;

$service->update($input);

Hope this helps.

like image 33
Zakaria Acharki Avatar answered Dec 04 '22 02:12

Zakaria Acharki