Tag Archives: laravel tutorials for beginners

Laravel inserting multiple records in table


You can insert multiple records into table at a time in laravel.


Syntax for laravel inserting multiple records in table

DB::table('table_name')->insert(array('data_array1','data_array2'....));

Laravel inserting multiple records in table Example

DB::table('users')->insert(array(
array("id"=>'1',"name"=>"john","email"=>"john@testgmail.com"),
array("id"=>'2',"name"=>"Jimmi","email"=>"jimmn@testgmail.com"),
array("id"=>'3',"name"=>"Kelly","email"=>"kelly@testgmail.com"),
array("id"=>'4',"name"=>"Mike","email"=>"mike@testgmail.com"),
array("id"=>'5',"name"=>"Steve","email"=>"steve@testgmail.com"),
);

or You can also use this :

$data1 = array("id"=>'1',"name"=>"john","email"=>"john@testgmail.com");
$data2 = array("id"=>'2',"name"=>"Jimmi","email"=>"jimmn@testgmail.com");
$data3 = array("id"=>'3',"name"=>"Kelly","email"=>"kelly@testgmail.com");
$data4 = array("id"=>'4',"name"=>"Mike","email"=>"mike@testgmail.com");
$data5 = array("id"=>'5',"name"=>"Steve","email"=>"steve@testgmail.com");
DB::table('users')->insert(array($data1,$data2,$data3,$data4,$data5,));

Laravel insert query


Laravel insert query : Laravel Insert Records Into A Table


Syntax for laravel insert Query

DB::table('table_name')->insert('data_array');

Laravel insert Query Example

DB::table('users')->insert(array("id"=>$uid,"name"=>$name,"email"=>$email));

or You can also use this :

$data = array("id"=>$uid,"name"=>$name,"email"=>$email);
DB::table('users')->insert($data);