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?
}
}
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"for yournameattributeif(!isset($yourval)){$yourval = 0;}although more commonly used in Laravel for these situations would be a ternary operator.$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.