I have a foreach loop that has a checkbox in it. I am not sure how to properly get that from the checkbox in the form over to the validation in the controller. I have the following in my view:
<table class="table">
<thead>
<tr>
<th scope="col">Select</th>
</tr>
</thead>
<tbody>
@foreach($plans as $plan)
<tr>
<td>
<input type='checkbox' name='Plan {{$plan->{'Plan ID'} }}' class='form-check-input' value='true'
@if(old($plan->{'Plan ID'},$plan->{'Plan ID'})=="true") checked @endif>
</td>
<td> {{$plan->{'Plan ID'} }}</td>
@endforeach
</tr>
</tbody>
</table>
I have the following in my controller:
$data = request()->validate ([
'current' => '',
'instructions' => '',
'Plan 1' => '',
'Plan 2' => '',
'Plan 3' => '',
'Plan 4' => '',
'Plan 5' => '',
'Plan 6' => '',
'Plan 7' => '',
'Plan 8' => '',
'Plan 9' => '',
'Plan 10' => '',
'Plan 11' => '',
'Plan 12' => '',
'Plan 13' => '',
'Plan 14' => '',
'Plan 15' => '',
]);
$plansubmission = PlanSubmission::find($id);
//$plansubmission->update($data);
$designs = MedicalDesignsToQuote::find($id);
if($designs==null){
$design = MedicalDesignsToQuote::create(['id'=>$id]);
$design->update($data);
}
else{
$designs->update($data);
I do not want to use an array for the 'name' attribute
Added this to controller:
$validator = Validator::make($request->all(), [
"list.*.id" => '', // Object exist validation
"list.*.value" => '',
'current' => '',
'instructions' => '',
]);
$bodyContent = $request->getContent($validator);
$plansubmission = PlanSubmission::find($id);
// $plansubmission->update($data);
$designs = MedicalDesignsToQuote::find($id);
if($designs==null){
$design = MedicalDesignsToQuote::create(['id'=>$id]);
// $design->update($data);
$design->update($validator);
}
else{
$designs->update($validator);
}
I have the following columns in my table:
id
current
instructions
Plan 1
Plan 2
Plan 3
Plan 4
Plan 5
.....
Plan 50
The user can select any or all of the 50 plan options with a checkbox. If, for example, Plan 5 is selected, the value for this given row within the table will be 'True' for plan 5.
So, to persist the data, I would expect I need to do something like this:
$formData = $request->all();
foreach ($formData['list'] as $planData) {
MedicalPlanDesignToQuote::updateOrCreate(
[$planData['id']] => $planData['value']],
);
}
If I had two columns called id and value, I suppose I could do this:
foreach ($formData['list'] as $planData) {
MedicalPlanDesignToQuote::updateOrCreate(
['id' => $planData['id']],
['value' => $planData['value']]
);
}
Is there anyway I can do this with my table structure?
plans => [0 => data, 1 => data], you will have easier way to loop them. And if you don't want plans have more than 15 items, you can just validate the plans with'array|max:15'.