samedi 18 juin 2016

Eloquent boolean property sometimes gets updated, sometimes it does not

I have the following mutator on my model:

/**
 * @param $value
 */
public function setBreadcrumbAttribute($value)
{
    $fix = is_null($value) ? 0 : (integer) $value;
    $this->attributes['breadcrumb'] = $fix;
}

And the following in my html:

{!! Form::checkbox('breadcrumb', 1, null, [
    'id' => 'breadcrumb'
]) !!}

And this is in my controller:

$page->update($request->only('title', 'body', 'breadcrumb'));

The breadcrumb property gets updated about 6 / 10 times of saving the form. The rest, it seems to be setting the correct property $this->attributes['breadcrumb'] but is not saving to the database for some reason.

This is driving me crazy.

P.S. The reason why I have is_null check is because if I deselect the checkbox, it return null to the request.

P.P.S. breadcrumb property is saved as boolean (tinyint) in the database.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire