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 save multiple field?

I have tables (marks,students,subjects,courses) marks belongs to (students,subjects,courses) and i inserting data in marks using foreach method here is my inserting controller method

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 ($s->courses->psubjects as $sub)
         {
         
            $data = [
                 
                 'course_id'  => $course_id,
                 'subject_id' => $sub->id,
                 'test_marks' => $request->test_marks,
                 'mid_marks'  => $request->mid_marks,
                 'end_marks'  => $request->end_marks,
                 'student_id' => $s->id,
                 'updated_at' => $datetime,
                 'created_at' => $datetime,

                ];

                Mark::insert($data);
        }

        return back();
    } 

and here is marks table

        Schema::create('marks', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('course_id')->unsigned()->index();
            $table->integer('subject_id')->unsigned()->index();
            $table->integer('student_id')->unsigned()->index();
            $table->float('test_marks')->nullable();
            $table->float('mid_marks')->nullable();
            $table->float('end_marks')->nullable();
            $table->timestamps();
        });
    }

and here is my inserting view

<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" name="test_marks" id="test_marks" data-parsley-type="number">
                    </div>                
                </td>

                <td class="col-md-3">                   
                    <div class="form-group">             
                    <input type="text" class="form-control" name="mid_marks" id="mid_marks" data-parsley-type="number">
                   </div>              
                </td>

                <td class="col-md-3">                   
                     <div class="form-group">             
                     <input type="text" class="form-control" name="end_marks" id="end_marks" data-parsley-type="number">
                     </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>

and when i insert one time here is my database marks table

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

problem is that how can i update this i mean what is route for getting updating form

thanks

0 likes
44 replies
vipin93's avatar
Level 13

@fabricecw here i noticed that when i try to insert data using request

'test_marks' => $request->test_marks [$value],

its not requesting the value i don't understand why this happening here is my new controller

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

                ];

                Mark::create($data);
        }

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

here is dd value

array:8 [▼
  "course_id" => 3
  "subject_id" => 1
  "student_id" => 4
  "test_marks" => null
  "mid_marks" => null
  "end_marks" => null
  "updated_at" => Carbon {#443 ▼
    +"date": "2017-02-24 14:20:38.581414"
    +"timezone_type": 3
    +"timezone": "Asia/Kolkata"
  }
  "created_at" => Carbon {#443 ▶}
]
fabricecw's avatar

Could you post a dd($request->test_marks)? Thanks.

fabricecw's avatar

Sorry, I forgot to check out the inserting view. What happens if you remove the foreach in the view (only to test it)? I think then it works.

vipin93's avatar
Level 13

@fabricecw here is noticed that if i skip any field of text_marks input field then its return null and when i insert all then its returning value


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

                ];

                
               dd($request->test_marks);
                Mark::create($data);
        }

        return back();

out put "null"

vipin93's avatar
Level 13

@fabricecw here is contrller

public function postmarkForm($reg_no = null,Request $request)
    {
        $s = Student::where('reg_no',$reg_no)->with('courses','courses.psubjects')->first();
        $id =  $s->courses->psubjects->pluck('id');
        $course_id = $s->courses->id;   
        $datetime = Carbon::now();
             
            $data = [
                 
                 'course_id'  => $course_id,
                 'subject_id' => $request->subject,
                 'student_id' => $s->id,
                 'test_marks' => $request->test_marks,
                 'mid_marks'  => $request->mid_marks,
                 'end_marks'  => $request->end_marks,                
                 'updated_at' => $datetime,
                 'created_at' => $datetime,

                ];
            
               dd($data);
              Mark::create($data);
        return back();
    }

and here is dd($data);

array:8 [▼
  "course_id" => 3
  "subject_id" => "6"
  "student_id" => 4
  "test_marks" => null
  "mid_marks" => null
  "end_marks" => null
  "updated_at" => Carbon {#443 ▶}
  "created_at" => Carbon {#443 ▼
    +"date": "2017-02-24 14:30:19.698631"
    +"timezone_type": 3
    +"timezone": "Asia/Kolkata"
  }
]

fabricecw's avatar

Did you remove the foreach-statement? And could you please dd($request->test_marks)?

vipin93's avatar
Level 13

@fabricecw from controller(remove foreach)? from controller i have removed foreach

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

                ];
                 dd($request->test_marks);

                Mark::insert($data);
    

        return back();

output

"null"

fabricecw's avatar

No, please remove the foreach-statement in the view.

vipin93's avatar
Level 13

@fabricecw if i removed from view then how can show all subject related to the student

fabricecw's avatar

It's just for testing... Your form has the same fields name multiple times (of cause the foreach-statement). That couldn't work.

vipin93's avatar
Level 13

@fabricecw if i remove foreach from view then I'm sure it will request the "test_marks"

fabricecw's avatar

With the foreach-statement, you generate multiple form fields with the same name:

<input type="text" class="form-control" name="test_marks" id="test_marks" data-parsley-type="number">

are multiple times in the form - but you don't post it as an array. With form arrays, you can do that.

In your case (I didn't test it - could have some typos :-):

View:

<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" name="rows[{{ $subject->id }}][test_marks]" id="{{ $subject->id }}_test_marks" data-parsley-type="number">
                    </div>
                </td>

                <td class="col-md-3">
                    <div class="form-group">
                        <input type="text" class="form-control" name="rows[{{ $subject->id }}][mid_marks]" id="{{ $subject->id }}_mid_marks" data-parsley-type="number">
                    </div>
                </td>

                <td class="col-md-3">
                    <div class="form-group">
                        <input type="text" class="form-control" name="rows[{{ $subject->id }}][end_marks]" id="{{ $subject->id }}_end_marks" data-parsley-type="number">
                    </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>

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 ($s->courses->psubjects as $sub)
         {
         
            $data = [
                 
                 'course_id'  => $course_id,
                 'subject_id' => $sub->id,
                 'test_marks' => $request->rows[$course_id]['test_mark'],
                 'mid_marks'  => $request->rows[$course_id]['mid_marks'],
                 'end_marks'  => $request->rows[$course_id]['end_mark'],
                 'student_id' => $s->id,
                 'updated_at' => $datetime,
                 'created_at' => $datetime,

                ];

                Mark::create($data);
        }

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

@fabricecw

errors

ErrorException in MarksSheetController.php line 44:
Undefined index: test_marks

in MarksSheetController.php line 44
at HandleExceptions->handleError(8, 'Undefined index: test_marks', 'C:\\laragon\\www\\gramyanchal\\app\\Http\\Controllers\\MarksSheetController.php', 44, array('reg_no' => 'GRN2017154837', 'request' => object(Request), 's' => object(Student), 'id' => object(Collection), 'course_id' => 3, 'datetime' => object(Carbon), 'sub' => object(Subject))) in MarksSheetController.php line 44
at MarksSheetController->postmarkForm('GRN2017154837', object(Request))
at call_user_func_array(array(object(MarksSheetController), 'postmarkForm'), array('reg_no' => 'GRN2017154837', object(Request))) in Controller.php line 55
at Controller->callAction('postmarkForm', array('reg_no' => 'GRN2017154837', object(Request))) in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), object(MarksSheetController), 'postmarkForm') in Route.php line 203

view

@foreach($s->courses->psubjects as $subject)
        <tbody>
            <tr>            
                <td class="col-md-3" >                      
                     <div class="form-group" >       
                      <select  class="form-control" id="subject_id" name="subject_id" 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" name="rows[{{ $subject->id }}][test_marks]" data-parsley-type="number">
                    </div>                
                </td>
                <td class="col-md-3">                   
                    <div class="form-group">             
                    <input type="text" class="form-control" id="mid_marks" name="rows[{{ $subject->id }}][mid_marks]" data-parsley-type="number">
                   </div>              
                </td>
                <td class="col-md-3">                   
                     <div class="form-group">             
                     <input type="text" class="form-control" id="end_marks" name="rows[{{ $subject->id }}][end_marks]" data-parsley-type="number">
                     </div>               
                </td>
            </tr>
        </tbody>
        @endforeach

controller


public function postmarkForm($reg_no = null,Request $request)
    {
        $s = Student::where('reg_no',$reg_no)->with('courses','courses.psubjects')->first();
        $id =  $s->courses->psubjects->pluck('id');
        $course_id = $s->courses->id;   
        $datetime = Carbon::now();
        foreach ($s->courses->psubjects as $sub)
         {       
            $data = [
                 
                 'course_id'  => $course_id,
                 'subject_id' => $sub->id,
                 'test_marks' => $request->rows[$course_id]['test_marks'],
                 'mid_marks'  => $request->rows[$course_id]['mid_marks'],
                 'end_marks'  => $request->rows[$course_id]['end_marks'],
                 'student_id' => $s->id,
                 'updated_at' => $datetime,
                 'created_at' => $datetime,

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

        return back();
    }
fabricecw's avatar

Could you please dd($request->rows)? Can't test the code at the moment, sorry...

vipin93's avatar
Level 13

@fabricecw

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

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

        return back();
    }

output

array:6 [▼
  1 => array:2 [▼
    "mid_marks" => null
    "end_marks" => null
  ]
  2 => array:2 [▼
    "mid_marks" => null
    "end_marks" => null
  ]
  3 => array:2 [▼
    "mid_marks" => null
    "end_marks" => null
  ]
  4 => array:2 [▼
    "mid_marks" => null
    "end_marks" => null
  ]
  5 => array:2 [▼
    "mid_marks" => null
    "end_marks" => null
  ]
  6 => array:2 [▼
    "mid_marks" => null
    "end_marks" => null
  ]
]
vipin93's avatar
Level 13

@fabricecw its my mistake

name="test_marks" name="rows[{{ $subject->id }}][test_marks]" data-parsley-type="number"  

double time name

fabricecw's avatar

I've updated my code in my post before. I'm confused why all the values are still NULL.

But could you check if it works now without an error?

vipin93's avatar
Level 13

@fabricecw here i try to input https://www.dropbox.com/s/7671honzo9mmdl3/2.PNG?dl=0 controller

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

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

        return back();

and output

array:8 [▼
  "course_id" => 3
  "subject_id" => 1
  "test_marks" => null
  "mid_marks" => null
  "end_marks" => null
  "student_id" => 4
  "updated_at" => Carbon {#443 ▶}
  "created_at" => Carbon {#443 ▶}
vipin93's avatar
Level 13

@fabricecw its only requesting mid_marks my all mid_marks fill with 500 if i give value 500 in only on field

array:8 [▼
  "course_id" => 3
  "subject_id" => 1
  "test_marks" => null
  "mid_marks" => "500"
  "end_marks" => null
  "student_id" => 4
  "updated_at" => Carbon {#443 ▶}
  "created_at" => Carbon {#443 ▶}
]

when i give different value to different field of mid_marks its return null

array:8 [▼
  "course_id" => 3
  "subject_id" => 1
  "test_marks" => null
  "mid_marks" => null
  "end_marks" => null
  "student_id" => 4
  "updated_at" => Carbon {#443 ▶}
  "created_at" => Carbon {#443 ▶}
]
fabricecw's avatar

Could you try to submit a very basic form and check if you receive the post data?

<form method="post" action="/acadmic/{{$s->reg_no}}/marks_upload">
    {{ csrf_field() }}
    <input type="text" name="test">
        <button type="submit" class="btn btn-primary btn-lg btn-block">Submit</button>

</form>

Controller:

public function postmarkForm($reg_no = null,Request $request)
    {
    dd($request->test);


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

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

        return back();
    }
fabricecw's avatar

Could you try this:

<form method="post" action="/acadmic/{{$s->reg_no}}/marks_upload">
    {{ 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->id }}_subject" name="rows[{{ $subject->id }}][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" name="rows[{{ $subject->id }}][test_marks]" id="{{ $subject->id }}_test_marks">
                    </div>
                </td>

                <td class="col-md-3">
                    <div class="form-group">
                        <input type="text" class="form-control" name="rows[{{ $subject->id }}][mid_marks]" id="{{ $subject->id }}_mid_marks">
                    </div>
                </td>

                <td class="col-md-3">
                    <div class="form-group">
                        <input type="text" class="form-control" name="rows[{{ $subject->id }}][end_marks]" id="{{ $subject->id }}_end_marks">
                    </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>
vipin93's avatar
Level 13

@fabricecw no luck

array:8 [▼
  "course_id" => 3
  "subject_id" => 1
  "test_marks" => "555"
  "mid_marks" => null
  "end_marks" => null
  "student_id" => 4
  "updated_at" => Carbon {#443 ▶}
  "created_at" => Carbon {#443 ▶}
]
fabricecw's avatar

I saw that I forgot to change the select field to an array. I've updated the code above. Please check it again.

Next

Please or to participate in this conversation.