5

I have form with checkbox

   @foreach($accounts as $acc)
         <input type="checkbox" value="1" name="account[{{ $acc->id }}]" @if($acc->published) checked @endif>
   @endforeach

How I can pass 0 value when checkbox is not checked?

My controller:

public function updateMon(Request $request) {
    $request->validate([
        'account' => 'required|array',
        'account.*' => 'integer'
    ]);

    foreach($account as $acc => $val) {
        dd($val); //how get 0?
    }
}
7
  • 3
    Please remember, checkboxes are not actually sent in the request if they are not checked. So you basically have to test for its existance using a isset(). If it exists, you can capture the value, if it does not exist, then default the value to whatever you want the Off setting to be Commented May 23, 2018 at 13:25
  • I think you are also missing a closing " for your name attribute Commented May 23, 2018 at 13:27
  • If the input is not checked it will not be sen through. So.. if(!isset($yourval)){$yourval = 0;} although more commonly used in Laravel for these situations would be a ternary operator. Commented May 23, 2018 at 13:27
  • Ok but validation? Commented May 23, 2018 at 13:27
  • Is this Boolean? Why not do $yourval = (!isset($yourval)) ? 0 : 1 ; OR $yourval = (!isset($yourval)) ? false : true ; OR $yourval = (isset($yourval)) ? true : false; then no need for validation as you are setting it. Commented May 23, 2018 at 13:34

5 Answers 5

8

checkboxes are only posted when they are checked, so in controller you can use this fragment

public function updateMon(Request $request) {
    $request->validate([
        'account' => 'required|array',
        'account.*' => 'integer'
    ]);


    $myVar = isset($request->account[0]) ? 1 : 0;
}
Sign up to request clarification or add additional context in comments.

Comments

7
@foreach($accounts as $acc)
         <input type="hidden" value="0" name="account[{{ $acc->id }}]">
         <input type="checkbox" value="1" name="account[{{ $acc->id }}]" @if($acc->published) checked @endif>
@endforeach

In controller

public function updateMon(Request $request) {
$request->validate([
    'account' => 'required|array',
    'account.*' => 'boolean'
]);

foreach($account as $acc => $val) {
    dd($val); //that returns 0 if not checked
}
}

If checked on checkbox than it gets value = 1 if not checked on checkbox gets value =0

Comments

1

Just add hidden input field.

<input type="hidden"  name="account[{{$acc->id }}]" value="0" >

Comments

0

Using Laravel Collective,

@foreach($accounts as $acc)

{{ Form::hidden('account['. $acc->id .']', false) }}
{{ Form::checkbox('display['.$acc->id.']', true,$acc->published ? true : false) }}

@endforeach

Comments

-1

Try this:

Request::macro('checkbox', function ($key, $checked = 1, $notChecked = 0) {
     return $this->input($key) ? $checked : $notChecked;
});

And use it like:

$model->property = $request->checkbox('input_name');

or

$model->property = $request->checkbox('input_name', 'yes', 'no');

depending on what values you need to store. You don't need do validate, you just need to check if the param is received.

I had to deal with the same issue, and I've tried many methods and I've found this one to be the most elegant.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.