CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   Laravel   Laravel 5 Insert Multiple Records in a Row With Eloquent

Laravel 5 Insert Multiple Records in a Row With Eloquent

July 9, 2021 by SNK

Laravel Multiple Insert With Eloquent

Today we will see laravel 5 insert multiple records with eloquent tutorial where you learn how to insert multiple data all at once in laravel.

In somecases it is very necessary and for the first time when i am using laravel and doing multiple insert i was confused and got difficult to do it.

Suppose, one student can read many subjects and you want to store the information of student who reads more than one subject and save it to different table as student_subject.

Same goes for the products, a product have many colors and you are saving their data into product colors table then when inserting data you need to select the product and select the colors and insert.

lets see the table below and get more clear of what i am trying to say,

laravel multiple insert with eloquent

In avobe table i want to select student who wants to study which subject so selecting student will be radio button while number of subjects would be checkboxes.

So we need to select from tbl_students and tbl_subjects and insert into third table tbl_student_subjects.

Lets create the model for 3 different tables.I have created my models as :-

  • tbl_students as to store the name of students.
  • tbl_subjects as to store the name of subjects.
  • tbl_subject_student to store which student study which subjects.

I have already created these table and added some dummy data by using faker and seeded it. If you want to use below are my codes that you can use it :-

Student table migration :-

tbl_student.php
PHP
1
2
3
4
5
6
7
8
public function up()
    {
        Schema::create('tbl_students', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

Subject table migration :-

tbl_subjects.php
1
2
3
4
5
6
7
8
public function up()
    {
        Schema::create('tbl_subjects', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->timestamps();
        });
    }

Student’s subject table migration :-

tbl_student_subjects.php
PHP
1
2
3
4
5
6
7
8
9
public function up()
    {
        Schema::create('tbl_student_subjects', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('student_id');
            $table->integer('subject_id');
            $table->timestamps();
        });
    }

Lets create the controllers and the routes. Jump into your command line create the controller using the make command and paste these code below :-

AdminController.php
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
 
namespace App\Http\Controllers;
 
use App\tbl_student;
use App\tbl_student_subject;
use App\tbl_subject;
use Illuminate\Http\Request;
 
class AdminController extends Controller
{
public function index() {
 
     // Get the student data
$students = tbl_student::all();
$subjects = tbl_subject::all();
 
return view('view_student',['students'=>$students,'subjects'=>$subjects]);
}
 
 
public function multipleInsert(Request $request) {
$data = $request->except('_token');
 
$subject_count = count($data['subject_id']);
for($i=0; $i < $subject_count; $i++){
 
$tss = new tbl_student_subject;
$tss->subject_id = $data['subject_id'][$i];
$tss->student_id = $data['student_id'];
$tss->save();
}
}
}

Now the routes. Go to routes/web.php and paste these line of codes :-

web.php
PHP
1
2
Route::get('/','AdminController@index');
Route::get('multiple-insert','AdminController@multipleInsert');

Finally the view file. Go to resources/views and create new file called view_student.blade.php

I always use bootstrap to make everything quick. Paste these code below :-

view_student.blade.php
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Multiple Insert Using Checkboxes</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" />
</head>
<body>
<div class="container" style="margin-top: 10px;">
<div class="row">
<div class="col-md-4">
<form action="{{ url('multiple-insert') }}" enctype="multipart/form-data">
{!! csrf_field() !!}
<table class="table table-bordered">
<thead>
<tr>
<th></th>
<th>S.N</th>
<th>Name</th>
</tr>
</thead>
<tbody>
@php($i=1)
@foreach($students->take(5) as $student)
<tr>
<td><input type="radio" name="student_id" value="{{$student->id}}"></td>
<td>{{$i}}</td>
<td>{{$student->name}}</td>
</tr>
@php($i++)
@endforeach
</tbody>
</table>
</div>
<div class="col-md-4">
<table class="table table-bordered">
<thead>
<tr>
<td></td>
<th>S.N</th>
<th>Subjects</th>
</tr>
</thead>
<tbody>
@php($i=1)
@foreach($subjects->take(5) as $subject)
<tr>
<td><input type="checkbox" name="subject_id[]" value="{{$subject->id}}"></td>
<td>{{$i}}</td>
<td>{{$subject->title}}</td>
</tr>
@php($i++)
@endforeach
</tbody>
</table>
<div class="form-group">
<button class="btn btn-primary btn-sm">
Insert
</button>
</div>
</form>
</div>
</div>
</div>
 
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" /></script>
</body>
</html>

SHARE ON
Buffer

Enjoyed this article?

Like us on

Laravel laravel 5 bulk insert laravel 5 create multiple records laravel 5 insert multiple records multiple array insert in laravel

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.