Bird
Raised Fist0
Laravelframework~20 mins

Seeding data in Laravel - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a seeder in Laravel?
easy
A. To fill the database with sample or default data automatically
B. To create database tables and columns
C. To handle user authentication
D. To manage application routes

Solution

  1. Step 1: Understand the role of seeders

    Seeders are designed to insert data into the database automatically for testing or initial setup.
  2. Step 2: Differentiate from other Laravel features

    Creating tables is done by migrations, authentication by middleware, and routes by route files, not seeders.
  3. Final Answer:

    To fill the database with sample or default data automatically -> Option A
  4. Quick Check:

    Seeder purpose = fill database with data [OK]
Hint: Seeders add data, migrations create tables [OK]
Common Mistakes:
  • Confusing seeders with migrations
  • Thinking seeders manage routes
  • Assuming seeders handle authentication
2. Which command creates a new seeder class in Laravel?
easy
A. php artisan make:seeder SeederName
B. php artisan create:seeder SeederName
C. php artisan seed:create SeederName
D. php artisan generate:seeder SeederName

Solution

  1. Step 1: Recall Laravel artisan commands for seeders

    The correct command to create a seeder is php artisan make:seeder SeederName.
  2. Step 2: Identify incorrect command formats

    Other options use wrong verbs like create, seed:create, or generate which are not valid artisan commands.
  3. Final Answer:

    php artisan make:seeder SeederName -> Option A
  4. Quick Check:

    Seeder creation command = make:seeder [OK]
Hint: Use 'make:seeder' to create seeders [OK]
Common Mistakes:
  • Using 'create:seeder' instead of 'make:seeder'
  • Confusing seeder creation with migration creation
  • Typing artisan commands with wrong verbs
3. Given this seeder code snippet, what will be the result after running 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']
  ]);
}
medium
A. Only one user will be added because insert accepts one record at a time
B. The users table will be emptied before adding Alice and Bob
C. An error will occur because the insert method requires a single array
D. Two new users named Alice and Bob will be added to the users table

Solution

  1. Step 1: Understand DB insert with array of arrays

    Passing an array of arrays to insert adds multiple records at once.
  2. Step 2: Confirm no truncation or errors in code

    The code inserts two users without deleting existing data, and the syntax is correct.
  3. Final Answer:

    Two new users named Alice and Bob will be added to the users table -> Option D
  4. Quick Check:

    Insert multiple records = array of arrays [OK]
Hint: Insert accepts array of arrays for multiple rows [OK]
Common Mistakes:
  • Assuming insert deletes existing data
  • Thinking insert only accepts one record
  • Expecting an error from multiple inserts
4. What is wrong with this seeder code?
public function run() {
  DB::table('products')->insert(
    ['name' => 'Book', 'price' => 10],
    ['name' => 'Pen', 'price' => 2]
  );
}
medium
A. The run method must return a value
B. The table name 'products' is invalid and causes an error
C. The insert method expects a single array, but two arrays are passed separately
D. The price values must be strings, not integers

Solution

  1. Step 1: Check insert method parameter format

    The insert method expects one array argument. Passing two separate arrays causes a syntax error.
  2. Step 2: Identify correct way to insert multiple rows

    Multiple rows should be inside a single array: an array of arrays.
  3. Final Answer:

    The insert method expects a single array, but two arrays are passed separately -> Option C
  4. Quick Check:

    Insert needs one array argument [OK]
Hint: Wrap multiple records in one array for insert [OK]
Common Mistakes:
  • Passing multiple arrays as separate arguments
  • Thinking run method must return a value
  • Assuming numeric values must be strings
5. You want to seed your database with 100 users having random names and emails using Laravel factories inside a seeder. Which code snippet correctly achieves this inside the run method?
hard
A. factory(User::class, 100)->make();
B. User::factory()->count(100)->create();
C. User::create()->factory()->count(100);
D. User::factory(100)->make();

Solution

  1. Step 1: Recall Laravel factory syntax for creating multiple records

    The correct syntax is User::factory()->count(100)->create(); to generate and save 100 users.
  2. Step 2: Identify incorrect factory usages

    User::create()->factory()->count(100); reverses method order, A uses old syntax, and D misses count method.
  3. Final Answer:

    User::factory()->count(100)->create(); -> Option B
  4. Quick Check:

    Factory count then create = correct [OK]
Hint: Use factory()->count(n)->create() to seed multiple records [OK]
Common Mistakes:
  • Using old factory syntax
  • Calling factory on create() result
  • Omitting count() method for multiple records
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard