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,
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 :-
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 :-
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 :-
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 :-
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 :-
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 :-
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> |
Leave a Reply