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

tanveerkhanwd's avatar

how to insert multiple rows in mysql using laravel

This is my view and data come into multipal form so please help me to insert all data into database with sql query

@csrf() ENROLL NO. STUDENT NAME CLASS ROLL NO. ABSENT/PRESENT @foreach($student as $stu)
  <tr style="border: 1px solid #f2f2f2;">
    <td><input type="text" name="enroll_no" class="form-control" value="{{$stu->enroll_no}}"></td>
    <td><input type="text" name="student_name" class="form-control" value="{{$stu->student_name}}"></td>
    <td>{{$loop->index+1}}</td>
    <td>
      <div class="form-check">
        <label class="form-check-label">
          <input style="zoom:2;" name="present" class="form-control" type="checkbox" value="P"> Absent/Present
        </label>
      </div>
    </td>
  </tr>
  @endforeach
</tbody>

my controller

public function sem1_C(Request $request) { $sem1= new Semester1; $sem1->enroll_no= $request->enroll_no; $sem1->student_name= $request->student_name; $sem1->present_Absent = $request->present; $sem1->save(); $class_name = ClassAdd::all(); $student = DB::table('students')->where('class_name', '=', 1)->get(); return view('teacher.sem1',compact('student')); }

0 likes
8 replies
tomopongrac's avatar

You can use insert method which accept array as argument

Semester1::insert($data);
Cronix's avatar

It needs to be an array, of arrays.

// insert these 2 records
$data = [
   ['something' => value1, 'somethingElse' => anotherValue1], // record 1
   ['something' => value2, 'somethingElse' => anotherValue2], // record 2
];

Model::insert($data);

all fields being inserted (something and somethingElse in this example) need to be in the $fillable array on the Model.

4 likes
tanveerkhanwd's avatar

thank you for your reply sir, but i want to save data in my database throw foreach loop so how i impliment foreach loop in my controller to save or insert record into database

please help me

Hamelraj's avatar
Hamelraj
Best Answer
Level 5
 @foreach($student as $stu)
  <tr style="border: 1px solid #f2f2f2;">
    <td><input type="text" name="enroll_no[]" class="form-control" value="{{$stu->enroll_no}}"></td>
    <td><input type="text" name="student_name[]" class="form-control" value="{{$stu->student_name}}"></td>
    <td>{{$loop->index+1}}</td>
    <td>
      <div class="form-check">
        <label class="form-check-label">
          <input style="zoom:2;" name="present[]" class="form-control" type="checkbox" value="P"> Absent/Present
        </label>
      </div>
    </td>
  </tr>
  @endforeach
</tbody>

Then in your controller

$enroll_no = $request->enroll_no;
$student_name = $request->student_name;
$present= $request->present;

foreach($enroll_no as $key => $no)
{
    $input['enroll_no'] = $no;
    $input['student_name '] = $student_name[$key];
    $input['present'] = $present[$key];

    Model::create($input);

}
3 likes
tanveerkhanwd's avatar

its working properly but i get an error that was\

SQLSTATE[HY000]: General error: 1364 Field 'student_name' doesn't have a default value (SQL: insert into semester1s (enroll_no, present_Absent, updated_at, created_at) values (16/0889, P, 2018-06-07 20:38:01, 2018-06-07 20:38:01))

tanveerkhanwd's avatar

config/database.php

file and set 'strict' => false for your connection.

if do this then its working but student name can not be entered so how can i resolve this error

tanveerkhanwd's avatar

i found my error its an small error and thank so much for supporting me

Please or to participate in this conversation.