DEV Community

Cover image for How to Insert Multiple Records in Laravel
Msh Sayket
Msh Sayket

Posted on

1

How to Insert Multiple Records in Laravel

In this tutorial, we will learn How to Insert Multiple Records in Laravel. 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. You Can Learn How to Download File in Laravel Livewire 3

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.

How to Insert Multiple Records in Laravel 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.");

    }

}
Enter fullscreen mode Exit fullscreen mode

I hope it can help you…
You Can learn More Tutorial from Dev Script School

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay