php - Add multiple data at once in laravel?

I have a field gender, age-group and patient_type. I want to store count for each patient type with age-group and gender. This is my form formData. And this is array of data I get arrayData. Also multiple age-group can be added.
For example, age-group=0-1, new_patient=>male=10,Female=60, old_patient=>male=50, female=20. How can I store this form data for multiple age-groups.
My Form code
<div class="newAgeGroup mt-5" id="newAgeGroup_0">
<div class="form-group row">
<div class="col-md-2">
<label for="">Select Age Group</label>
</div>
<div class="col-md-5 mb-3">
<select name="age_id[]" class="form-control" id="">
<option value="">Select Age Group</option>
@foreach($ages as $age)
<option value="{{$age->id}}">{{$age->start_age}}
-{{$age->end_age}}</option>
@endforeach
</select>
</div>
<div class="col-md-5">
<button type="button" onclick="addAgeGroup();"
class="btn btn-primary rounded-0 float-right addAgeGroup">Add Age Group
</button>
</div>
</div>
<table class="table table-bordered">
<thead>
<tr>
<th></th>
@foreach($patient_types as $patient_type)
<th>
{{$patient_type->name}}
<input type="hidden" name="patient_type_id[]" value="{{ $patient_type->id }}">
</th>
@endforeach
</tr>
</thead>
<tbody>
@foreach($genders as $gender)
<tr>
<th>
{{$gender->name}}
<input type="hidden" name="gender_id[]" value="{{ $gender->id }}">
</th>
@foreach($patient_types as $patient_type)
<?php
$name =
"number_".$patient_type->id."_".$gender->id ;
?>
<td>
<input type="text" name="{{ $name }}[]" class="form-control">
</td>
@endforeach
</tr>
@endforeach
</tbody>
</table>
</div>
And this is what I have tried.
foreach ($request->input('age_id') as $key => $value) {
foreach ($request->input('patient_type_id') as $pt => $ptvalue) {
foreach ($request->input('gender_id') as $gkey => $gvalue) {
foreach ($request->input('number_'.$ptvalue.'_'.$gvalue) as $nkey => $nvalue) {
$report = new Report();
$report->admin_id = $this->admin()->id;
$report->category_id = $request->center_type;
$report->center_id = $request->center_id;
$report->dateTime = $request->dateTime;
$report->nationality = $request->nationality;
$report->type = 'OPD';
$report->age_id = collect($request->get('age_id')[$key])->implode(',');
$report->patient_type_id = collect($request->get('patient_type_id')[$pt])->implode(',');
$report->gender_id = collect($request->get('gender_id')[$gkey])->implode(',');
$report->number = collect($request->get('number_'.$ptvalue.'_'.$gvalue)[$nkey])->implode(',');
$report->save();
}
}
}
}
Answer
Solution:
If I understand well, you want a single age group to have multiple records associated with it.
If that's the case I recommend you, to use relation in the database to achieve that.
For example these two tables:
Age group | age group attributes |
---|---|
id | id |
age | age_group_id |
gender | |
patient type |
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
HTML
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.