Laravel 9 Insert Multiple Records In Database

Websolutionstuff | Dec-16-2022 | Categories : Laravel MySQL

In this article, we will see laravel 9 insert multiple records in the database. Here, we will learn how to insert multiple records in the database in laravel 8 and laravel 9.

In laravel, you can insert multiple records into the database using model and laravel query builder. We will give you an example of using the model and laravel DB query.

So, let's see laravel multiple data insert into a database, insert data in the database in laravel 7/8/9.

Example:

In this example, we will create a multidimensional array and insert multiple records using the insert() function.

$createMultipleUsers = [
    ['name'=>'Admin','email'=>'[email protected]', 'password' => bcrypt('admin@123')],
    ['name'=>'Guest','email'=>'[email protected]', 'password' => bcrypt('Guest@123')],
    ['name'=>'Super-Admin','email'=>'[email protected]', 'password' => bcrypt('Sup-Admin@123')]
];

User::insert($createMultipleUsers); // Here we use Laravel Eloquent to insert multiple records
\DB::table('users')->insert($createMultipleUsers); // Here we are use Query Builder to insert multiple records

 

Example: Insert Multiple Records into the Database in Laravel 9 using Seeder

In this example, we will create a laravel seeder to create a dummy record and insert multiple records in laravel 9. So, follow the below steps and create a seeder.

Step 1: Create Laravel Seeder

In this step, we will create a laravel seeder using the following command.

php artisan make:seeder UserSeeder

 

Step 2: Edit UserSeeder

Now, we will edit the UserSeeder.php file. So, add the following code to that file.

database/seeders/UserSeeder.php

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Faker\Generator as Faker;
use App\Models\User;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $faker = Faker::create();

        foreach (range(1,25) as $index) {
            User::create([
                'name' => $faker->name,
                'email' => $faker->email,
                'job_description' => $faker->text,
                'company' => $faker->company
            ]);
        }
    }
}

 

Step 3: Run Seeder

In this step, we will run the laravel seeder using the following command.

php artisan db:seed --class=UserSeeder

 


You might also like:

Recommended Post
Featured Post
Carbon Add Minutes In Laravel
Carbon Add Minutes In Laravel

In this example, we will see carbon add minutes in laravel. Here we will give you a simple example of laravel carbo...

Read More

Dec-11-2020

How to Upgrade PHP 8.0 to 8.1 in Ubuntu
How to Upgrade PHP 8.0 to 8.1...

Hey there! I recently needed to upgrade my PHP version from 8.0 to the latest 8.1 on my Ubuntu server. It's a smart...

Read More

Nov-03-2023

How to Send Bulk Mail Using Queue in Laravel 9
How to Send Bulk Mail Using Qu...

In this article, we will see how to send bulk mail using queue in laravel 9. Laravel queue is used for sending bulk...

Read More

Mar-15-2022

How To Generate PDF and Send Email In Laravel 8
How To Generate PDF and Send E...

In this tutorial we will see how to generate pdf and send email in laravel 8. For generating PDF file we will use l...

Read More

Dec-20-2021