0
0
Laravelframework~10 mins

Seeding data in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Seeding data
Create Seeder Class
Define run() Method
Write Data Insert Logic
Run Seeder via Artisan
Data Inserted into Database
Verify Data in DB
This flow shows how Laravel seeds data: create a seeder class, write data insertion code, run it, and verify data in the database.
Execution Sample
Laravel
php artisan make:seeder UserSeeder

// In UserSeeder.php
public function run() {
  DB::table('users')->insert(['name' => 'Alice']);
}

php artisan db:seed --class=UserSeeder
This code creates a seeder, inserts a user named Alice, and runs the seeder to add data to the database.
Execution Table
StepActionCode/CommandEffectDatabase State
1Create seeder classphp artisan make:seeder UserSeederUserSeeder.php file createdNo new data
2Write run() methodDB::table('users')->insert(['name' => 'Alice']);Insert command readyNo new data
3Run seederphp artisan db:seed --class=UserSeederSeeder executedUser 'Alice' added
4Verify dataCheck users tableData presentUsers table has Alice
5ExitNo further commandsSeeding completeFinal DB state with Alice
💡 Seeder run completes, data inserted into users table.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
users tableemptyempty[{"name": "Alice"}][{"name": "Alice"}]
Key Moments - 2 Insights
Why does running 'php artisan make:seeder UserSeeder' not insert data immediately?
Creating the seeder class only generates the file; data insertion happens only when you run the seeder with 'php artisan db:seed --class=UserSeeder' as shown in step 3.
What happens if you run the seeder multiple times without clearing the database?
Each run inserts the same data again, causing duplicates. This is because the insert command adds new rows every time, as seen in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the database state after step 2?
AUsers table contains Alice
BUsers table is empty
CUsers table contains multiple users
DUsers table is deleted
💡 Hint
Check the 'Database State' column for step 2 in the execution table.
At which step does the actual data get inserted into the database?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the step where 'Seeder executed' and 'User Alice added' appear in the execution table.
If you run the seeder twice without clearing the database, what will happen?
AData will be duplicated
BNo change after first insert
CDatabase will be cleared automatically
DSeeder will fail to run
💡 Hint
Refer to the key moment about running the seeder multiple times causing duplicates.
Concept Snapshot
Laravel Seeding Data:
1. Create seeder: php artisan make:seeder NameSeeder
2. Define run() with DB insert logic
3. Run seeder: php artisan db:seed --class=NameSeeder
4. Data inserts into DB
5. Repeat runs add duplicates unless cleared
Full Transcript
Seeding data in Laravel involves creating a seeder class using artisan, writing the data insertion logic inside the run method, running the seeder to insert data into the database, and verifying the data is present. The process starts with generating the seeder file, then adding insert commands, and finally executing the seeder command. Running the seeder multiple times without clearing the database will add duplicate entries. This visual trace shows each step and how the database state changes from empty to containing the seeded data.