How to Insert Multiple Rows in Laravel

Throughout this Laravel Insert Multiple records in Database tutorial, we are goind to share how to create or insert multiple rows (data) to the database using Seeder.

More than time we need to insert multiple records in the database for testing the app so, we can use database seeder or query builder to Insert multiple records in the database using multi-dimensional array. Here we create a multidimensional array and save data in the database using insert or create methods.
How to insert multiple rows in database Laravel Example

Laravel Insert Multiple Rows in Database

We can add the static key and value to saving data in the database, So first create usersTableSeeder to seeding data in database run the following command.

php artisan make:seeder UsersTableSeeder

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

<?php

use Illuminate\Database\Seeder;
use App\User;

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

  public function run()
    {

      User::truncate();

        $users =  [
            [
              'name' => 'Super Admin',
              'email' => 'superadmin@gmail.com',
              'password' => '123456',
            ],
            [
              'name' => 'Account Admin',
              'email' => 'accountadmin@gmail.com',
              'password' => '13456',
            ],
            [
              'name' => 'Project Admin',
              'email' => 'projectadmin@gmail.com',
              'password' => '13456',
            ],
            [
              'name' => 'Client Admin',
              'email' => 'clientadmin@gmail.com',
              'password' => '13456',
            ]
          ];

          User::insert($users);

    }
}

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

php artisan db:seed --class=UsersTableSeeder

If you have multiple database seeds in laravel then you need run one command php artisan db:seed, but there you need to add your all seeds in db seeds just same as below.

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{

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

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

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

php artisan:db seed

Insert Multiple records in the Database 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 checkout how can we use faker to generate fake records.

<?php
  
use Illuminate\Database\Seeder;
use App\User;
use Illuminate\Support\Facades\Hash;

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

Learn Also: Generate Fake Data using Faker and Factory in Laravel

After running the php artisan db:seed –class=usersTableSeeder you can check the dummy records created in your users table.

5 thoughts on “How to Insert Multiple Rows in Laravel”

Leave a Comment