Laravel insert multiple records in Database

In this tutorial, you will learn Laravel insert multiple records in Database Using Seeder. We will use the database seeder class to insert multiple rows into the database. Laravel has more than one way to insert the dummy or test data such as Seeder, Faker and factory classes. You can generate dummy data using model Factories and fakers to create fake data, for developing and testing your app.

How to Insert Multiple Records in Database Laravel – Using Seeder

So let’s see the below step by step by step example on how to create How to Insert Multiple Records in Database Laravel using the database seeder class.

#1 Download Laravel App

First, you need to download a new laravel application using the following command;

composer create-project --prefer-dist laravel/laravel laravel-datatables

Next, go inside the app:

cd laravel-datatables

#2 Setup Database Credentials

Next, open the .env file and update the database credentials on it;

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db name
DB_USERNAME=db user name
DB_PASSWORD=db password

#3 Run Migration

When you install a new laravel application, laravel already generates some migration like users, etc, So we need to run the migration command to generate the tables in our database. So run the below command.

php artisan migrate

#4 Create Laravel Seeder

Now, create usersTableSeeder for inserting multiple records into the database in laravel;

php artisan make:seeder UserTableSeeder

#5 Update code in Seeder

Next, navigate app/migrations/seeder directory and put the following code in UserTableSeeder.php file.

<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use App\Models\User;

class UserTableSeeder extends Seeder
{
     /**
     * Run the database seeds.
     *
     * @return void
     */

  public function run()
    {

        User::truncate();

        $users =  [
            [
              'name' => 'Super Admin',
              'email' => 'superadmin@gmail.com',
              'password' => Hash::make('1245678')
            ],
            [
              'name' => 'Account Admin',
              'email' => 'accountadmin@gmail.com',
              'password' =>  Hash::make('1245678')
            ],
            [
              'name' => 'Project Admin',
              'email' => 'projectadmin@gmail.com',
              'password' =>  Hash::make('1245678')
            ],
            [
              'name' => 'Client Admin',
              'email' => 'clientadmin@gmail.com',
              'password' =>  Hash::make('1245678')
            ]
          ];

          User::insert($users);

    }
}

Next, execute the below seed command for inserting multiple records in the database.

php artisan db:seed --class=UserTableSeeder

If we add the seeder inside the DatabaseSeeder.php file, just like below then we need to run the following command.

<?php

use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{

    /**
     * Seed the application's database.
     *
     * @return void
     */

    public function run()
    {
        $this->call(UserTableSeeder::class);
    }
}

Add this to your Database Seeder and run the below command, your all seeds run automatically.

php artisan:db seed

Laravel Insert Multiple records using Faker

You can generate dummy data using model Factories and faker to create fake data (with relations etc) for developing and testing your app. Here you need to use faker class for generation testing users. Let’s check out how can we use faker to generate fake records.

<?php
  
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Faker\Generator as Faker;
use App\Models\User;

class UserTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        for ($i=0; $i < 50; $i++) { 
	    	User::create([
	            'name' => str_random(8),
	            'email' => str_random(12).'@mail.com',
	            'password' => Hash::make('12345678'),
	        ]);
    	}
    }
}

Run the php artisan db:seed –class=userTableSeeder command and then check the dummy records created in your users table.

I hope you are now satisfied with their concept of how to insert multiple records into the database in laravel application. We can use the seeder to insert directly as your know all about the above example.

Let us know if you still have any query so we can help you. Thank you.

Leave a Comment