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

vipin93's avatar
Level 13

How to update multiple field?

I successfully create the data with single request problem is that how can now update them problem with what it should be the route of "geteditform" and "postupdate" form should be my created controller

 public function postmarkForm($reg_no = null,Request $request)
    {

        $s = Student::where('reg_no',$reg_no)->with('courses','courses.psubjects')->first();    
        $course_id = $s->courses->id;
        $datetime = Carbon::now();
        foreach ($request->subject as $key => $v)
         {       
            $data = [
                 
                 'course_id'  => $course_id,
                 'subject_id' => $v,
                 'test_marks' => $request->test_marks [$key],
                 'mid_marks'  => $request->mid_marks [$key],
                 'end_marks'  => $request->end_marks [$key],
                 'student_id' => $s->id,
                 'updated_at' => $datetime,
                 'created_at' => $datetime

                ];
          
                Mark::create($data);
        }

        return back();
    }

and view for saving form

<form method="post" action="/acadmic/{{$s->reg_no}}/marks_upload" data-parsley-validate ="">
        {{ csrf_field() }}
      <table class="table table-bordered  table-hover">
        <thead>
            <tr>
                <th  style="text-align: center;">Subject</th>
                <th  style="text-align: center;">Test Marks</th>
                <th  style="text-align: center;">Mid Marks</th>
                <th  style="text-align: center;">End Marks</th>
            </tr>
        </thead>
        @foreach($s->courses->psubjects as $subject)
        <tbody>
            <tr>            
                <td class="col-md-3" >                      
                     <div class="form-group" >       
                      <select  class="form-control" id="subject[]"  name="subject[]" required="">
                      <option value="{{ $subject->id}}">{{ $subject->name }}</option>
                      </select>
                     </div>                
                </td>                   
                <td class="col-md-3">                   
                    <div class="form-group">              
                     <input type="text" class="form-control"  id="test_marks[]"  name="test_marks[]" data-parsley-type="number" data-parsley-range="[0, 25]">
                    </div>                
                </td>
                <td class="col-md-3">                   
                    <div class="form-group">             
                    <input type="text" class="form-control" id="mid_marks" name="mid_marks[]" data-parsley-type="number" data-parsley-range="[0, 50]">
                   </div>              
                </td>
                <td class="col-md-3">                   
                     <div class="form-group">             
                     <input type="text" class="form-control" id="end_marks" name="end_marks[]" data-parsley-type="number" data-parsley-range="[0, 100]">
                     </div>               
                </td>
            </tr>
        </tbody>
        @endforeach
      </table>
                   
     <div class="col-md-4 col-md-offset-4">
     <br>
        <button type="submit" class="btn btn-primary btn-lg btn-block">Submit</button>
     </div>     
    </form>

I have relationship marks belongs to (students, courses, subjects) and students belongs to(courses) and courses and subjects have pivot table(course_subject) here in this pic i have inserted value all in single request , i confused that what should be the route(getform and update form because here have 6 column value

https://www.dropbox.com/s/rgd5748ap6tqavl/1.PNG?dl=0

0 likes
25 replies
joaomantovani's avatar

Sorry, I didn't get your doubt, are you trying to create a multiple save and your pivot table is becoming empty? or are you having problems in routes?

Ps: your dropbox link is not working

joaomantovani's avatar

Maybe

Route

Route::post('/acadmic/marks/{id}/update_form', yourcontroller@update);

Controller

 public function update($id, $reg_no = null,Request $request)
    {

        $s = Student::where('reg_no',$reg_no)->with('courses','courses.psubjects')->first();    
        $course_id = $s->courses->id;
        $datetime = Carbon::now();

        foreach ($request->subject as $key => $v)
         {       
            $data = [
                 
                 'course_id'  => $course_id,
                 'subject_id' => $v,
                 'test_marks' => $request->test_marks [$key],
                 'mid_marks'  => $request->mid_marks [$key],
                 'end_marks'  => $request->end_marks [$key],
                 'student_id' => $s->id,
                 'updated_at' => $datetime,
                 'created_at' => $datetime

                ];
          
                Mark::find($id)->update($data);
        }

        return back();
    }
vipin93's avatar
Level 13

@joaomantovani this will only accept single "id" so if i will followed this will not return value of other what about [1,2,3,4,5] (which inserted at once)

joaomantovani's avatar

You can do 5 other requests or pass an array of ids

Mark::find($id[key])->update($data);
Snapey's avatar

Think about the structure of your data.

If all the marks are associated with one course, then the route could be

/student/{student}/course/{course_id}/marks

or if the course_id is already unique to the student, then just

/course/{course_id}/marks

Then in the controller, get all marks related to the course and iterate over them in the form.

vipin93's avatar
Level 13

@Snapey

<form method="post" action="/acadmic/{{$s->marks['id']}}/{{$s->reg_no}}/marks_edit" data-parsley-validate ="">
        {{ csrf_field() }} {{ method_field('PATCH') }}
      <table class="table table-bordered  table-hover">
        <thead>
            <tr>
                <th  style="text-align: center;">Subject</th>
                <th  style="text-align: center;">Test Marks</th>
                <th  style="text-align: center;">Mid Marks</th>
                <th  style="text-align: center;">End Marks</th>
            </tr>
        </thead>
        @foreach($marks as $mark)
        <tbody>
            <tr>            
                <td class="col-md-3" >                      
                     <div class="form-group" >       
                      <select  class="form-control" id="subject[]"  name="subject[]" required="">
                      <option value="{{ $mark->subjects['id']}}">{{ $mark->subjects['name'] }}</option>
                      </select>
                     </div>                
                </td>                   
                <td class="col-md-3">                   
                    <div class="form-group">              
                     <input type="text" class="form-control"  id="test_marks[]"  name="test_marks[]" data-parsley-type="number" value="{{ $mark->test_marks }}" data-parsley-range="[0, 25]">
                    </div>                
                </td>
                <td class="col-md-3">                   
                    <div class="form-group">             
                    <input type="text" class="form-control" id="mid_marks" name="mid_marks[]" data-parsley-type="number" value="{{ $mark->mid_marks }}" data-parsley-range="[0, 50]">
                   </div>              
                </td>
                <td class="col-md-3">                   
                     <div class="form-group">             
                     <input type="text" class="form-control" id="end_marks" name="end_marks[]" value="{{ $mark->end_marks }}"  data-parsley-type="number"  data-parsley-range="[0, 100]">
                     </div>               
                </td>
            </tr>
        </tbody>
        @endforeach
      </table>
                   
     <div class="col-md-4 col-md-offset-4">
     <br>
        <button type="submit" class="btn btn-primary btn-lg btn-block">Submit</button>
     </div>     
    </form>

i got errors as expected

Undefined index: id (View: 
Snapey's avatar

How could I possibly answer without knowing what $s contains?

vipin93's avatar
Level 13

@Snapey actually $s has Student , and Student have marks

my get_edit_form controller

public function geteditmarkForm($reg_no = null, $course_id=null)
    {
        $s = Student::where('reg_no',$reg_no)->with('courses','courses.psubjects')->first();


        $marks =  Mark::with('subjects')->where('course_id', $course_id)
                ->whereHas('students',function($query) use($reg_no){ 

                 $query->where('reg_no', $reg_no);

                })->get();

        return view('marks_sheet.marks_sheet_edit_form',compact('s','marks'));
    }

and my Student Model

public function marks()
    {
        return $this->hasMany(Mark::class);
    }

and my Mark Model

public function courses()
    {
        return $this->belongsTo(Course::Class,'course_id');
    }

    public function subjects()
    {
        return $this->belongsTo(Subject::Class,'subject_id');
    } 

    public function students()
    {
        return $this->belongsTo(Student::Class,'student_id');
    }
Snapey's avatar

You want to save the marks you have already?

And you want to edit the marks for a specific course in one go?

Or all the marks for one student?

action="/student/{{$s->id}}/course/{{ $s->course->id}}/marks_edit"
vipin93's avatar
Level 13

@Snapey yes already saved but how can update (what is post controller) as @joaomantovani said i can pass in a array

Mark::find($id[key])->update($data);

so how can i find and update and i want "want to edit the marks for a specific course(of student and student next year have different course) in one go"

Snapey's avatar

What's wrong with what I suggested?

1 like
vipin93's avatar
Level 13

@Snapey

action="/acadmic/{{$s->reg_no}}/{{$s->course_id}}/marks_edit"

and controller

 public function updateeditmarkForm($reg_no=null, $course_id = null, Request $request)
    {

        $s = Student::where('reg_no',$reg_no)->with('courses','courses.psubjects')->first();

        $marks = Mark::with('subjects')->where('course_id', $course_id)
                ->whereHas('students',function($query) use($reg_no){ 

                 $query->where('reg_no', $reg_no);

                })->get();    
        $course_id = $s->courses->id;
        $datetime = Carbon::now();
        foreach ($request->subject as $key => $v)
         {       
            $data = [
                 
                 'course_id'  => $course_id,
                 'subject_id' => $v,
                 'test_marks' => $request->test_marks [$key],
                 'mid_marks'  => $request->mid_marks [$key],
                 'end_marks'  => $request->end_marks [$key],
                 'student_id' => $s->id,
                 'updated_at' => $datetime
                 

                ];
            //dd($data);
                $marks->update($data);
        }

        return back();
    }

errrosrs as expected

Method update does not exist.

and @fabricecw sorry for that

Snapey's avatar

method update does not exist

public function updateeditmarkForm(

notice anything different here?

vipin93's avatar
Level 13

u mean i have to use

($id, $course_id, Request $request)
n31l's avatar

did you figure this out vipin?

n31l's avatar

btw what Snapey had said which is obvious is that

Your route is pointing to the Update Method where you instead have updateeditmarkForm so, you'd have to change your route to ControllerName@updateeditmarkForm

Please or to participate in this conversation.