Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
83 views
in Technique[技术] by (71.8m points)

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();
                    }
                }
            }
        }
question from:https://stackoverflow.com/questions/65944128/add-multiple-data-at-once-in-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...