jhutto

Best way to process $request data from data submitted on form with foreach loop table

Posted 1 year ago by jhutto

What's the best way to take a table of data, show it on a form to edit using a foreach loop to create a table of input data. Then process it in the controller to update table in database.

To create the form I pull all the Tuition's from a db table that are associated with a Class ID.

$Tuitions = DB::table('tuitions')->where('ClassID','=', $coursesid)->get();

Then send it to the view.

Here's the form data creating a table of input in the view.

<div class="form-group col-md-12">
                                    <table id="Tuitions" class="table table-striped table-bordered">
                                            <thead><tr>
                                            <th style="width: 10%;text-align:center;">TuitionType</th>
                                            <th style="width: 30%;text-align:center;">TuitionDesc</th>
                                            <th style="width: 20%;text-align:center;">ClassFee</th>
                                            <th style="width: 20%;text-align:center;">HomeOfficeFee</th>
                                            <th style="width: 20%;text-align:center;">MaterialsFee</th>
                                            </tr></thead     
                                          
                                            @foreach ($Tuitions as $key=>$value)
                                            <tr>
                                                <input type="hidden" id="TuitionId" name="Tuitionid[]" value="{{ $value->id }}">
                                                <td><input type="text" id="TuitionType" name="TuitionType[]" style="text-align:center;"  value="{{ $value->TuitionType }}"></td>
                                                <td><input type="text" id="TuitionDesc" name="TuitionDesc[]"  style="width:350px" value="{{ $value->TuitionDesc }}"></td>
                                                <td><input type="text" id="ClassFee" name="ClassFee[]" style="text-align:center;" value="{{ $value->ClassFee }}"></td>
                                                <td><input type="text" id="HomeOfficeFee" name="HomeOfficeFee[]" style="text-align:center;" value="{{ $value->HomeOfficeFee }}"></td>
                                                <td><input type="text" id="MaterialsFee" name="MaterialsFee[]" style="text-align:center;" value="{{ $value->MaterialsFee }}"></td>
                                            @endforeach
                                              
                                              </label>
                                           
                                    </table>
                                </div>

Now here's the dd($request); The top portion of the request is data gathered in the same form that is class data. I have not problem pulling this information and updating it.

It's the data starting with Tuitionid... How do I extract this to update the tuitions table.

+request: ParameterBag {#64 ▼
    #parameters: array:15 [▼
      "_method" => "Patch"
      "_token" => "o6e8a4M2EnVsoUU4x87KbXtxFj1QXb9noiUjg3VG"
      "SemesterID" => "36"
      "Courseid" => "372"
      "ClassName" => "L&L Early Childhood"
      "instructor" => "10"
      "day" => "Thursday"
      "Time" => "6:30 PM"
      "Location" => "Vogt"
      "Tuitionid" => array:5 [▼
        0 => "4521"
        1 => "4522"
        2 => "4523"
        3 => "4524"
        4 => "4525"
      ]
      "TuitionType" => array:5 [▼
        0 => "SINGLE"
        1 => "COUPLE"
        2 => "S-STAFF"
        3 => "C-STAFF"
        4 => "GIFT"
      ]
      "TuitionDesc" => array:5 [▼
        0 => "Single"
        1 => "couple per person"
        2 => "single-staff"
        3 => "couple-staff"
        4 => "gift"
      ]
      "ClassFee" => array:5 [▼
        0 => "90.00"
        1 => "75.00"
        2 => "40.00"
        3 => "32.50"
        4 => "0.00"
      ]
      "HomeOfficeFee" => array:5 [▼
        0 => "0.00"
        1 => "0.00"
        2 => "0.00"
        3 => "0.00"
        4 => "0.00"
      ]
      "MaterialsFee" => array:5 [▼
        0 => "0.00"
        1 => "0.00"
        2 => "0.00"
        3 => "0.00"
        4 => "0.00"
      ]
    ]
  }

Anyone ?? Please help.

Please sign in or create an account to participate in this conversation.