with
old('amenities', $propertyAmenityArr)
$propertyAmenityArr should be an array of the existing property amenities. If there are not any because the object is new then it should be set to an empty array.
I want to submit a checkbox with update for which I am using this code and it's working fine
<div class="form-row">
<?php
$amenityMo = \App\Models\Amenity::orderBy('amenity_name','ASC')->get();
$amenity = $amenityMo->split($amenityMo->count()/1);
?>
@foreach($amenity as $row)
<div class="col-md-2 mb-2">
@foreach($row as $j => $amenityCl)
<div class="form-check">
<input class="form-check-input" type="checkbox" value="{{ $amenityCl->id; }}" name="amenities[]" @if(old('amenities') !== null) @checked(in_array($amenityCl->id, old('amenities', []))) @else @checked(in_array($amenityCl->id, $propertyAmenityArr)) @endif id="defaultCheck{{ $amenityCl->id; }}">
<label class="form-check-label" for="defaultCheck{{ $amenityCl->id; }}">
{{ $amenityCl->amenity_name; }}
</label>
</div>
<?php $j++; ?>
@endforeach
</div>
@endforeach
@if($errors->has('amenities'))
<div class="error text-danger fs-6">{{ $errors->first('amenities') }}</div>
@endif
</div>
My controller
$propertyAmenityArr = array();
$propertyAmenity = PropertyAmenity::select('amenity_id')->where('property_id',$id)->where('status',0)->get();
foreach ($propertyAmenity as $key => $value) {
$propertyAmenityArr[] = $value->amenity_id;
}
return view('pro.edit',compact('propertyAmenityArr'));
The problem is that the second parameter you give the the old() function is used when the first value is null. So when you do old('amenities', $propertyAmenityArr) and no old value for 'amenities' is found, $propertyAmenityArr is used. So I want it to write in such a way that the codes work even if there is no old value for 'amenities' is found, means it should remain unchecked. I figured out three possibilities in it
@babai9 OK, I see your issue.
The problem that is unique to checkboxes is that they are not sent at all when not checked.
So when you submit the form with all checkboxes cleared, then there is no old('amenities') value at all because it is not part of the form data. Its not related to the PR you linked to. When there is no old value then the defaults are used.
One option is to include a hidden field that is always sent.
somewhere in your form, before the checkboxes;
input type="hidden" name="amenities[]" />
This will always be included, so when saving the results to the database you may need ignore any null member of the array.
edit: an improved option
input type="hidden" name="amenities" /> // no square braces
and
@checked(in_array($amenity->id, old('amenities', $propertyAmenityArr) ??[])) />
The null safe operator is used for the case were the previous form submission was all checkboxes cleared in which case the hidden field is used and causes old amenities to be null. The nullsafe operator swaps this for an empty array.
The benefit of the edit version is that the hidden field is not present when some checkboxes are checked.
Please or to participate in this conversation.