Reputation: 290
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?
Upvotes: 3
Views: 3281
Reputation: 466
You need to create an Option Group and Option tables to handle all your lists.
For example, you need a Location List and a Restaurant List.
Add a parent of Location List in Option Group and the child in Option and join them in Option.php to handle all your list in the future.
public static function getOfficeLocationList(){
return Option::where('group_id', config('constants.OPT_GROUP_OFFICE_LOC'))->orderBy('name','ASC')->get();
}
In my example, group_id is the ID of the parent list, in that way you can get all the location option you stored.
In your controller:
$locationList = Option::getOfficeLocationList();
In your blade:
@foreach($locationList as $item)
your input checkbox with $item->name and $item->id
@endforeach
That's it: Throw the formData in your validation :)
Upvotes: 0
Reputation: 808
First things first:
You have an error in your example. The @endforeach
needs to be moved one column down.
Second:
You need to understand, what you are exactly doing here. You deal with an 1-n relationship between some kind of list and the list items. Ergo, you need to build your post/get body accordingly.
An example for a single table row could be:
<tr>
<td>
<Input type="hidden" value="{{$plan->{'Plan ID'} }}" name="list[][id]">
<Input type="checkbox" value="true" name="list[][value]">
</td>
<td>
{{$plan->{'Plan ID'} }}
</td>
</tr>
Now you are able to iterate over items accordingly, independent from their id.
Now you are able to use request validation as stated here: https://laravel.com/docs/master/validation#validating-arrays
In our case:
"list.*.id" => "...", // Object exist validation
"list.*.value" => "..."
Now how to pass everything to objects and persist them:
You need to be aware, you can't just pass anything into your objects. Right now you pass a validator. Instead use the validation only vor validation. Laravel will take care of returning a response.
$this->validate($request, [
'current' => required'',
'instructions' => 'required',
"list.*.id" => "required", // Object exist validation
"list.*.value" => "required"
]);
Afterwards you get your data from your request object:
$formData = $request->all();
We stored our information in the key 'list' (see html). So you will find it in your request object. There are multiple "plans" stored in our list. Now you need to iterate:
foreach ($formData['list'] as $planData) {
Plan::updateOrCreate(
['id' => $planData['id']],
['value' => $planData['value']]
);
}
Of course you need to also save other objects/create relationships if there are any.
If you want to stay with your table structure (you may should rework it: keyword BCNF), you are still able/need to iterate:
$dataArray = [];
foreach ($formData['list'] as $planData) {
$dataArray['Plan ' . $planData['id']] = $planData['value'];
}
$dataArray['current'] = $formData['current'];
$dataArray['instructions'] = $formData['instructions'];
// This way do not need to use create
$designs = MedicalDesignsToQuote::updateOrCreate(
['id' => $id],
$dataArray
);
Upvotes: 4