0
0
Laravelframework~20 mins

Seeding data in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Seeder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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.
A5 users will be added to the database.
B10 users will be added because the seeder runs twice automatically.
CAn error will occur because the count method is invalid here.
DNo users will be added because the factory is not called correctly.
Attempts:
2 left
💡 Hint
Think about what the count(5) method does in a factory call.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a seeder class in Laravel?
Select the option that shows a valid Laravel seeder class syntax.
A
&lt;?php

use Illuminate\Database\Seeder;

class ProductSeeder extends Seeder
{
    public function run()
    {
        // seeding logic
    }
}
B
&lt;?php

class ProductSeeder
{
    public function run()
    {
        // seeding logic
    }
}
C
&lt;?php

use Illuminate\Database\Seeder;

class ProductSeeder extends Seeder
{
    public function execute()
    {
        // seeding logic
    }
}
D
&lt;?php

use Illuminate\Database\Seeder;

class ProductSeeder extends Seeder
{
    public function run()
    // missing braces
}
Attempts:
2 left
💡 Hint
Look for the correct method name and class inheritance.
🔧 Debug
advanced
2: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.
AThe 'title' field is not allowed to be null, but it is missing.
BThe 'user_id' field is missing in the create array, causing a NOT NULL constraint violation.
CThe Post model does not have a create method.
DThe seeder class is missing the run method.
Attempts:
2 left
💡 Hint
Check the database table columns and their constraints.
state_output
advanced
2: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.
A0 categories, because firstOrCreate does not insert anything.
B4 categories total, because the seeder runs twice and adds duplicates.
C2 categories total, because firstOrCreate prevents duplicates.
DAn error occurs because firstOrCreate is used incorrectly.
Attempts:
2 left
💡 Hint
Think about what firstOrCreate does when the record exists.
🧠 Conceptual
expert
2:00remaining
Which statement best describes Laravel's seeding and factory relationship?
Choose the most accurate description of how Laravel seeders and factories work together.
ASeeders use factories to generate fake data, but factories cannot be used outside seeders.
BSeeders and factories are unrelated; seeders manually insert data without factories.
CFactories are only for testing and cannot insert data into the database during seeding.
DFactories define how to create model instances with fake data; seeders call factories to insert data into the database.
Attempts:
2 left
💡 Hint
Consider the roles of factories and seeders in Laravel's data generation.