Support the ongoing development of Laravel.io →
Requests Eloquent Forms

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

my 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_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();
    }

my 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->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"  id="{{ $subject->id }}_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="{{ $subject->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="{{ $subject->id }}_end_marks" name="rows[{{ $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>

my 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();
        });

here is problem when i try submit the for my test_marks, mid_marks, end marks returning null here is dd($data)

array:8 [▼
  "course_id" => 3
  "subject_id" => 1
  "test_marks" => "600"
  "mid_marks" => "500"
  "end_marks" => null
  "student_id" => 4
  "updated_at" => Carbon {#443 ▶}
  "created_at" => Carbon {#443 ▶}
]
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

vipin733 vipin733 Joined 23 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.