Challenge - 5 Problems
Laravel Seeder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output after running this Laravel seeder?
Consider this Laravel seeder code snippet. What will be the number of users in the database after running it once?
Laravel
<?php use Illuminate\Database\Seeder; use App\Models\User; class UserSeeder extends Seeder { public function run() { User::factory()->count(5)->create(); } } // Assume the users table is empty before running.
Attempts:
2 left
💡 Hint
Think about what the count(5) method does in a factory call.
✗ Incorrect
The count(5) method tells Laravel to create 5 user records. Since the users table is empty before running, 5 new users will be added.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a seeder class in Laravel?
Select the option that shows a valid Laravel seeder class syntax.
Attempts:
2 left
💡 Hint
Look for the correct method name and class inheritance.
✗ Incorrect
Laravel seeders must extend Seeder and define a run() method with braces. Option A follows this pattern.
🔧 Debug
advanced2:00remaining
Why does this seeder fail with a SQL error?
This seeder code throws a SQL error when run. What is the most likely cause?
Laravel
<?php use Illuminate\Database\Seeder; use App\Models\Post; class PostSeeder extends Seeder { public function run() { Post::create([ 'title' => 'First Post', 'content' => 'Welcome to the blog!' ]); } } // The posts table has a NOT NULL 'user_id' foreign key column.
Attempts:
2 left
💡 Hint
Check the database table columns and their constraints.
✗ Incorrect
The posts table requires a 'user_id' because of a foreign key constraint. Not providing it causes the SQL error.
❓ state_output
advanced2:00remaining
What is the state of the database after running this seeder twice?
Given this seeder code, what will be the total number of categories in the database after running it two times in a row?
Laravel
<?php use Illuminate\Database\Seeder; use App\Models\Category; class CategorySeeder extends Seeder { public function run() { Category::firstOrCreate(['name' => 'Laravel']); Category::firstOrCreate(['name' => 'PHP']); } } // The categories table is empty before the first run.
Attempts:
2 left
💡 Hint
Think about what firstOrCreate does when the record exists.
✗ Incorrect
firstOrCreate checks if a record exists with the given attributes. If yes, it returns it without creating a new one. So running twice adds only 2 unique categories.
🧠 Conceptual
expert2:00remaining
Which statement best describes Laravel's seeding and factory relationship?
Choose the most accurate description of how Laravel seeders and factories work together.
Attempts:
2 left
💡 Hint
Consider the roles of factories and seeders in Laravel's data generation.
✗ Incorrect
Factories define blueprints for fake data. Seeders call these factories to create and insert data into the database during seeding.