I have some data to update status with checkbox
Controller
public function editTargetInProject(Request $request)
{
$arr = $request->id;
foreach($arr as $id){
$status = $request->status;
if($status){
Target::findOrFail($status)->update(['status' => "DONE"]);
}
}
}
<div class="form-group">
{{ Form::label('target_name', 'Target Name') }}
{{ Form::text('target_name', $target->name, ['class' => 'form-control', 'placeholder' => 'Target Name' ]) }}
{{ Form::text('id[]', $target->id, ['class' => 'form-control', 'placeholder' => 'Target Name' ]) }}
</div>
<div class="form-group">
{{ Form::label('status', 'Status') }}
<br>
@if($target->status == "DONE")
<input type="checkbox" class="minimal" name="status_{{$target->id}}" id="status" checked>
@else
<input type="checkbox" class="minimal" name="status_{{$target->id}}" id="status">
@endif
</div>
When I update the data all of the data updated to "DONE", I just want to update data where the checkbox is TRUE.
Try to add an value attribute to your inputs
<input type="checkbox" VALUE="DONE" class="minimal" name="status_{{$target->id}}" id="status" checked>
and in your for loop, you are missing an id after $request->status_{id}
Try the following
public function editTargetInProject(Request $request)
{
$arr = $request->id;
foreach($arr as $id){
$status_id = 'status_' . $id;
$status = $request->$status_id;
if($status){
Target::findOrFail($status)->update(['status' => "DONE"]);
}
}
}