4

I am using a checkbox in a form, for some reasons I can't store the value of the checkbox status (checked or unchecked).

I am using form model binding.

My form is:

{!! Form::model($profile, ['method' => 'PATCH', 'action' => ['ProfilesController@update', $profile->id]]) !!}

<div class="form-group">
  {!! Form::label('wifi', 'Wifi') !!}
  {!! Form::checkbox('wifi','yes', $profile->wifi) !!}Wifi
</div>

{!! Form::close() !!}

My Schema:

$table->boolean('wifi')->nullable();

But I also tried it with an integer

I can't figure out what I am doing wrong

2 Answers 2

7

Your this piece of code

{!! Form::checkbox('wifi','yes', $profile->wifi) !!}Wifi

is generating this

<input checked="checked" name="wifi" type="checkbox" value="yes">

Which means that you are sending the value yes to the server but your column data type is not varchar/text. You set it to boolean.

update your code to this because you are using form model binding so no need to popluate it, laravel will do this for you.

{!! Form::checkbox('wifi') !!} Wifi

Also, include your wifi key in fillable and casts array. like so

protected $fillable = [ ..., 'wifi' ];

protected $casts = [ 'wifi' => 'boolean' ];

Note: your schema code

$table->boolean('wifi')->nullable;

nullable is not a property, it is a function. so update it as well

$table->boolean('wifi')->nullable();

After that refersh your database migration

php artisan migrate:refresh
Sign up to request clarification or add additional context in comments.

10 Comments

Hi Zayn, thank's. I used your example, and updated to nullable(). Still the same, the state is not saved to the DB
Yes I have refreshed my migration
Okay and in your model, have you put wifi in your fillable and also in the casts array? like this protected $casts = [ 'wifi' => 'boolean' ];
Zayn, great!!!! Thank you so much. I never heard about the Attribute Casting. That did the trick
Awesome! I'm glad it helped :)
|
0

It depends on how are you trying to persist this data.

If you're using save() method, do something like this:

$model->wifi = isset($request->wifi);

PS: I guess it should be ->nullable()

4 Comments

Hi Alexey, I am using: public function update(ProfileRequest $request, $id) { $profile = Profile::findOrFail($id); $profile->update($request->all()); return redirect('backend/profile'); }
Then try to do this $request->wifi = isset($request->wifi); right before $profile->update($request->all());. And also, in your case, you should have wifi in a $fillable array of Profile model.
ok, wifi is in a fillable array and I updated my Controller to: public function update(ProfileRequest $request, $id) { $profile = Profile::findOrFail($id); $request->wifi = isset($request->wifi); $profile->update($request->all()); return redirect('backend/profile'); } But still the same :(
What exactly doesn't work? Do you have any error? If not, what does 'dd(iseet($request->wifi))` says when checkbox is checked and when it's not?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.