Laravel 11 insert multiple records in Database using Laravel Seeder, Faker and factory classes. So let’s see the below step by step example how to Insert Multiple Records in Database Laravel using the database seeder class.
Laravel 11 insert Data in Database using Seeder
First see how we insert the dummy/fake data in database using seeder class:
#1 Download Laravel App
First, you need to install a fresh laravel app;
composer create-project --prefer-dist laravel/laravel laravel-app
Next, go to the app path:
cd laravel-app
#2 Setup Database
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
Now, run the migration command to generate the tables in our database.
php artisan migrate
#4 Create Laravel Seeder file
Run the following command to create seeder file.
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.
If you want to generate the dummy/fake data using faker then replace the seeder class code with the blow one.
<?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 this example help you to insert multiple records in laravel 11;