Error retrieving a checked Checkbox in Laravel as a boolean

laravel boolean
laravel checkbox validation
validate array laravel
laravel model validation
laravel errors
laravel validation default value
laravel datetime validation
laravel validation after

I'm a bit new to laravel and I'm working on a laravel application which has a checkbox field which should have a boolean value of 1 when the user checks the checkbox, and 0 when the checkbox is unchecked.

I want to retrieve the boolean value as either 1 or 0 and save in a db.

Please assist?

View

<form method="POST" action="{{ route('b2c.getplans') }}" id="travel_form"  accept-charset="UTF-8">

    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <div class="check-now {{ $errors->has('spouse') ? ' has-error' : '' }}">
        <h1 class="cover-travel">I am travelling with</h1>
        <label class="spouse-me">
            <h1 class="pumba">Spouse</h1>
            <input id="spouse" type="checkbox" name="spouse">
            <span class="checkmark" name="spouse"></span>
        </label>
        @if ($errors->has('spouse'))
            <span class="help-block">
                <strong>{{ $errors->first('spouse') }}</strong>
            </span>
        @endif
    </div>

    <button type="submit" class="form-b3c"> Get Plans</button>
</form>

Controller

 public
    function validatePlanEntries(Request $request)
    {

        $validation = $this->validate($request, [
            'WithSpouse' => (\Input::has('spouse')) ? 1 : 0;
        ]
}

1st way is send correct value from frontend side

you can add jquery or javascript at frontend side on change event of checkbox :

<input type="checkbox" name="checkbox" id="myCheckbox" />

<script>
$(document).on('change','#myCheckbox',function(){
    if($(this).is(':checked')){
       $('#myCheckbox').val(1);
    }else{
       $('#myCheckbox').val(0);
    }
});
</script>

at your backend side , now you can check :

 $yourVariable=$request->input('checkbox');

2nd way is only check at your backend

you will get your checkbox value=on if it checked

if($request->input('checkbox')=='on'){
   $yourVariable=1;
}else{
   $yourVariable=0;
  }

you can use ternary condition as well , like :

$yourVariable = $request->input('checkbox')=='on' ? 1:0;

The field is a boolean and when I try to retrieve from the request it returns "on" It's not working using 'active' => ['required', 'boolean'], in the validator. object), false if not. https://laravel.com/docs/5.8/requests#retrieving-input Keep in mind that for a checkbox, a required rule means it must be checked. Migrated "boolean" type, checkbox submits "on" instead of 1 Posted 2 years ago by sanuglia In a Laravel 5.4 install, I created a migration with a boolean (turned tinyint(1) ) field named "enabled".


You don't have to validate a checkbox value. because if its checked it sends the value of on, and if it's not checked it doesn't send anything.

So, all you need is that get whether the check box is checked or not, you can do as follow.

to retrieve the boolean value of the checkbox,

Controller

// since you haven't provide any codes in the controller what
// are you gonna do with this value,
// I will juts catch it to a variable.

$isChecked = $request->spouse == 'on'

I've got a couple of checkboxes and I'm not sure how to go about getting it '​morphClass' => null public 'exists' => boolean true array (size=4) 30 and this is the error i get Laravel make a checkbox checked by default. In my database I have a table Manufacturer and one of the fields is status (boolean) Migration is set to boolean but the database saves it at 0 an 1 . When I try to edit the filed I set the boolean to checked.


To retrieve checkbox value from request as boolean:

$yourModel->with_spouse = (bool) $request->spouse;

If checkbox is checked then it's value (on by default) will be passed to request and casting non-empty string to boolean will give you true. If checkbox is not checked then spouse key won't be passed to request at all, so $request->spouse will return null. Casting null to boolean will result in false (in PHP true is int 1 and false is int 0, which is exactly what you want).

I ran into a problem where boolean validation for GET parameters An unchecked checkbox is never submitted. Feat/docker laravel #10. How can I retrieve the value of a checkbox in a Laravel form and store it to a database? Posted 2 years ago by 18laravel I'm trying to get the value of a checkbox via a form in laravel and store it's value to a boolean column in the table.


I know that HTTP does not send a value if a checkbox or radio button is I'd like to suggest that Laravel always synthesizes this value if necessary when as synthesizing a boolean value when processing the POST data. Put this before the checkbox, with NAMEOFFIELD being the same as the the checkbox, so by default the value is 0 and will be overwritten with 1 if the checkbox gets checked level 2 laurencei


<input checked="checked" name="my_checkbox" type="checkbox" This third argument should evaluated to a boolean value ( true or false ). I know that HTTP does not send a value if a checkbox or radio button is unchecked, but for the sake of consistency and simplicity, I'd like to suggest that Laravel always synthesizes this value if necessary when processing the POST data so that the client only has to handle the cases false and true for each button.


In this post I'll show how to set a Laravel checkbox value to checked the Form::​checkbox wants a boolean to decide whether to check the  The most concise screencasts for the working developer, updated daily. There's no shortage of content at Laracasts. In fact, you could watch nonstop for days upon days, and still not see everything!