How to insert multiple records in table Laravel 5

How to insert multiple records in table Laravel 5

In this post we will give you information about How to insert multiple records in table Laravel 5. Hear we will give you detail about How to insert multiple records in table Laravel 5And how to use it also give you demo for it if it is necessary.

When you are working with hasMany relationship then you will need to insert multiple record at a time in a table.

Here you will find how many ways you can insert multiple record in a table and how to insert multiple records in a linked table(in case of hasMany relationship).

Syntax are :

  1. DB::table('your_table_name')->insert(array('array_data1','array_data2'....));
DB::table('your_table_name')->insert(array('array_data1','array_data2'....));
  1. DB::table('products')->insert(array(
  2. array("id"=>'1',"name"=>"onlinecode","details"=>"Learning Portal"),
  3. array("id"=>'2',"name"=>"demo.onlinecode","details"=>"Find running example"),
  4. );
DB::table('products')->insert(array(array("id"=>'1',"name"=>"onlinecode","details"=>"Learning Portal"),array("id"=>'2',"name"=>"demo.onlinecode","details"=>"Find running example"),);

OR

  1. $data1=array("id"=>'1',"name"=>"onlinecode","details"=>"Learning Portal");
  2. $data2=array("id"=>'2',"name"=>"demo.onlinecode","details"=>"Find running example");
  3. DB::table('products')->insert(array($data1,$data2));
$data1 = array("id"=>'1',"name"=>"onlinecode","details"=>"Learning Portal");$data2 = array("id"=>'2',"name"=>"demo.onlinecode","details"=>"Find running example");DB::table('products')->insert(array($data1,$data2));

This was example using DB query builder now i will insert multiple records using laravel eloquent model.

  1. $data=array(
  2. array("id"=>'1',"name"=>"onlinecode","details"=>"Learning Portal"),
  3. array("id"=>'2',"name"=>"demo.onlinecode","details"=>"Find running example"),
  4. //...
  5. );
  6. Product::insert($data);// Eloquent
$data = array(    array("id"=>'1',"name"=>"onlinecode","details"=>"Learning Portal"),    array("id"=>'2',"name"=>"demo.onlinecode","details"=>"Find running example"),    //...);Product::insert($data); // Eloquent

Now I will tell you how to insert multiple records in table when you are working with hasMany relationship.

Suppose you have a table User and relatively a Comment table and User table has many comment and each comment belongs to user so when you are inserting multiple comments related models user then you can use saveMany method.

  1. $user= AppUser::find(1);
  2. $user->comments()->saveMany([
  3. new AppComment(['message'=>'First comment.']),
  4. new AppComment(['message'=>'Second comment.']),
  5. ]);
$user = AppUser::find(1);$user->comments()->saveMany([    new AppComment(['message' => 'First comment.']),    new AppComment(['message' => 'Second comment.']),]);

You can try this in your application when you need to insert bulk records at a time in a table using either query builder or using eloquent.

Hope this code and post will helped you for implement How to insert multiple records in table Laravel 5. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

  +  10  =  16

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US