Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jhutto's avatar

Form patch submit only returning last record of @foreach in form

I've create a edit form that is posting with patch method. I have a @foreach that creates the items in a table. When the form is submitted and I check the $request output it only returns the last entry from the @foreach loop. Can someone show me how to get all the values for each line item in the @foreach table

This is a simplified version of the form.

<!-- form user info -->
<div class="card card-outline-secondary">
<div class="card-header">
<h3 class="mb-0">Course Tuition</h3>
</div>
<div class="card-body">
 <form method="Post" action="/courses/{{ $Courseid }}" >
     @method('Patch')
     @csrf
                              
<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>
        <td><input type="text" id="TuitionType" name="TuitionType" style="text-align:center;"  value="{{ $value->id }} {{ $value->TuitionType }}"></input></td>
            <td><input type="text" id="TuitionDesc" name="TuitionDesc"  style="width:350px" value="{{ $value->id }} {{ $value->TuitionDesc }}></td>
           <td><input type="text" id="ClassFee" name="ClassFee" style="text-align:center;" value="{{ $value->id }} {{ $value->ClassFee }}></td>
            <td><input type="text" id="HomeOfficeFee" name="HomeOfficeFee" style="text-align:center;" value="{{ $value->id }} {{ $value->HomeOfficeFee }}>/td>
            <td><input type="text" id="MaterialsFee" name="MaterialsFee" style="text-align:center;" value="{{ $value->id }} {{ $value->MaterialsFee }}></td>
                    @endforeach
              </table>
</div>
             <div class="form-row">
              <div class="form-group col-lg-9" Style="height:50px;margin-left: 25px;">
               <input type="submit" class="btn btn-primary btn-lg" value="Save Changes">
                  <a href="/registrations/courses/{{$course[0]->SemesterID}}" class="btn btn-secondary btn-lg" role="button">Cancel</a>
               </div>
</div>
</form>
                        
                        </div>
                    </div>

0 likes
5 replies
Cronix's avatar

show how you are retrieving $Tuitions in the controller and passing it to the view.

topvillas's avatar
Level 46

You're giving all the inputs the same name (and id). Use name="name[]" if you want something to be posted to PHP as an array.

jhutto's avatar

Sorry for the late response... Here's how I'm sending all the data to the form...

public function edittest($id, $coursesid)
    {
        
        

        $course = DB::table('somascourses')
            ->join('Instructors', 'somascourses.Instructorid', '=', 'Instructors.id')
            ->select('*','somascourses.id as course_id')
            ->where('somascourses.SemesterID','=','$id')
            ->orwhere('somascourses.id','=', $coursesid)
            ->get();
        
        //dd($course);

        $instructors = DB::table('instructors')->where('Viewable', '=', '1')->get();
        $semesterid = $id;
        $Courseid = $coursesid;

        $days = [
            'Sunday' => 'Sunday',
            'Monday' => 'Monday',
            'Tuesday' => 'Tuesday',
            'Wednesday' => 'Wednesday',
            'Thursday' => 'Thursday',
            'Friday' => 'Friday',
            'Saturday' => 'Saturday',
        ];

        $InstructorSelected = $course[0]->Instructorid;
       ($InstructorSelected);
       //dd($course);
       //dd($instructors);

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

       //dd($Tuitions);
      
       return view('courses.edittest', compact('course', 'instructors', 'semesterid', 'Courseid', 'days', 'InstructorSelected', 'Tuitions'));



    }

jhutto's avatar

Thanks topvillas... That was it...

Viernes's avatar

@TOPVILLAS - I know it's an old topic but i face the same problem but when i use the same solution it's gave me

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR)
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in C:\laragon\www\off\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 745

Please or to participate in this conversation.