Laravel Eloquent Insert Multiple Rows Example

Oct 04, 2022 . Admin



Hello Friends,

This tutorial will provide an example of laravel's eloquent insert multiple rows examples. you can understand the concept of laravel creating multiple records. I explained simply about laravel insert multiple records eloquent. This tutorial will give you a simple example of how to create multiple records in laravel. Here, Creating a basic example of laravel's eloquent insert of multiple rows.

If we work on a big project and then we may be required to add multiple rows to a database using laravel eloquent. Laravel provides an insert method for bulk records created on DB.

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

You can use this example with the versions of laravel 6, laravel 7, laravel 8, and laravel 9.

Example:

we will create MultipleRecordController. so you can see the below code with output:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

class MultipleRecordController extends Controller
{

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function multiplerecords()
    {
        $myItems = [
            ['title'=>'HD Topi','description'=>'Mywebtuts.com '],
            ['title'=>'HD Topi 2','description'=>'Mywebtuts.com  2'],
            ['title'=>'HD Topi 3','description'=>'Mywebtuts.com  3']
        ];

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

It will help you...

#Laravel