Seeding data helps you quickly fill your database with sample information. This makes testing and development easier without typing data manually.
Seeding data in Laravel
php artisan make:seeder SeederName
// Inside database/seeders/SeederName.php
public function run()
{
DB::table('table_name')->insert([
'column1' => 'value1',
'column2' => 'value2',
]);
}
// Run all seeders
php artisan db:seedSeeder classes go inside database/seeders folder.
Use php artisan db:seed to run all seeders or php artisan db:seed --class=SeederName for one.
php artisan make:seeder UsersTableSeeder
// In UsersTableSeeder.php
public function run()
{
DB::table('users')->insert([
'name' => 'Alice',
'email' => 'alice@example.com',
'password' => bcrypt('password123'),
]);
}public function run()
{
User::factory()->count(10)->create();
}UsersTableSeeder without running all seeders.php artisan db:seed --class=UsersTableSeederThis seeder adds one user named John Doe with a hashed password. You run it with the artisan command shown in the comment.
<?php namespace Database\Seeders; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; class UsersTableSeeder extends Seeder { public function run() { DB::table('users')->insert([ 'name' => 'John Doe', 'email' => 'john@example.com', 'password' => Hash::make('secret123'), ]); } } // To run this seeder, use: // php artisan db:seed --class=UsersTableSeeder
Always hash passwords before inserting users to keep data safe.
You can call other seeders inside DatabaseSeeder.php to organize data setup.
Use factories for large amounts of fake data to save time.
Seeding fills your database with sample data automatically.
Create seeders with php artisan make:seeder and run them with php artisan db:seed.
Use factories for easy generation of many fake records.
Practice
Solution
Step 1: Understand the role of seeders
Seeders are designed to insert data into the database automatically for testing or initial setup.Step 2: Differentiate from other Laravel features
Creating tables is done by migrations, authentication by middleware, and routes by route files, not seeders.Final Answer:
To fill the database with sample or default data automatically -> Option AQuick Check:
Seeder purpose = fill database with data [OK]
- Confusing seeders with migrations
- Thinking seeders manage routes
- Assuming seeders handle authentication
Solution
Step 1: Recall Laravel artisan commands for seeders
The correct command to create a seeder isphp artisan make:seeder SeederName.Step 2: Identify incorrect command formats
Other options use wrong verbs like create, seed:create, or generate which are not valid artisan commands.Final Answer:
php artisan make:seeder SeederName -> Option AQuick Check:
Seeder creation command = make:seeder [OK]
- Using 'create:seeder' instead of 'make:seeder'
- Confusing seeder creation with migration creation
- Typing artisan commands with wrong verbs
php artisan db:seed --class=UserSeeder?
public function run() {
DB::table('users')->insert([
['name' => 'Alice', 'email' => 'alice@example.com'],
['name' => 'Bob', 'email' => 'bob@example.com']
]);
}Solution
Step 1: Understand DB insert with array of arrays
Passing an array of arrays toinsertadds multiple records at once.Step 2: Confirm no truncation or errors in code
The code inserts two users without deleting existing data, and the syntax is correct.Final Answer:
Two new users named Alice and Bob will be added to the users table -> Option DQuick Check:
Insert multiple records = array of arrays [OK]
- Assuming insert deletes existing data
- Thinking insert only accepts one record
- Expecting an error from multiple inserts
public function run() {
DB::table('products')->insert(
['name' => 'Book', 'price' => 10],
['name' => 'Pen', 'price' => 2]
);
}Solution
Step 1: Check insert method parameter format
The insert method expects one array argument. Passing two separate arrays causes a syntax error.Step 2: Identify correct way to insert multiple rows
Multiple rows should be inside a single array: an array of arrays.Final Answer:
The insert method expects a single array, but two arrays are passed separately -> Option CQuick Check:
Insert needs one array argument [OK]
- Passing multiple arrays as separate arguments
- Thinking run method must return a value
- Assuming numeric values must be strings
run method?Solution
Step 1: Recall Laravel factory syntax for creating multiple records
The correct syntax isUser::factory()->count(100)->create();to generate and save 100 users.Step 2: Identify incorrect factory usages
User::create()->factory()->count(100); reverses method order, A uses old syntax, and D misses count method.Final Answer:
User::factory()->count(100)->create(); -> Option BQuick Check:
Factory count then create = correct [OK]
- Using old factory syntax
- Calling factory on create() result
- Omitting count() method for multiple records
Ready to prove it?
Pass the test and this pattern moves unknown
