Bird
0
0

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📝 Application Q15 of 15
Laravel - Database Basics and Migrations
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?
Afactory(User::class, 100)->make();
BUser::factory()->count(100)->create();
CUser::create()->factory()->count(100);
DUser::factory(100)->make();
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes