ItSolutionStuff.com

How to Insert Multiple Records in Laravel?

By Hardik Savani • November 5, 2023
Laravel

Hey Artisan,

In this comprehensive tutorial, we will learn laravel eloquent insert multiple rows. you can see laravel eloquent bulk insert. This example will help you laravel insert multiple records eloquent. In this article, we will implement a laravel eloquent insert multiple rows.

If we work on big project and then we maybe require to add multiple rows on database using laravel eloquent. Laravel provide insert method for bulk records create on db.

In bellow example you can see i use multidimensional $myItems array variable and that insert multiple records same time using DB::insert(). So let's see and try this.

Example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

class ItemController extends Controller

{

/**

* Display a listing of the resource.

*/

public function index(Request $request)

{

$myItems = [

['title'=>'HD Topi','description'=>'It solution stuff'],

['title'=>'HD Topi 2','description'=>'It solution stuff 2'],

['title'=>'HD Topi 3','description'=>'It solution stuff 3']

];

DB::table("items")->insert($myItems);

dd("Added multiple items to database.");

}

}

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Eloquent Find by Column Name Example

Read Now →

How to Find Multiple Ids using Laravel Eloquent?

Read Now →

Laravel Eloquent without() and withOnly() Method Example

Read Now →

Laravel Eloquent doesntHave() Condition Example

Read Now →

Laravel Eloquent Group By with Month and Year Example

Read Now →

Laravel Copy Record using Eloquent Replicate Example

Read Now →

Laravel Eloquent firstOrNew Example

Read Now →

Laravel Has Many Through Eloquent Relationship Tutorial

Read Now →

Laravel Eloquent Group By with Multiple Columns Example

Read Now →

Laravel Multiple Files Download with Response Example

Read Now →

Laravel Eloquent Order By Random Row Example

Read Now →

Laravel 5.2 multi auth example using Auth guard from scratch

Read Now →

Laravel Eloquent Inner Join with Multiple Conditions Example

Read Now →

Laravel Order By with Column Value Example

Read Now →