Performance: Factory definitions
Factory definitions affect the speed of database seeding and test data generation, impacting initial load and test execution time.
<?php use App\Models\User; User::factory()->count(1000)->create(); // Laravel batches inserts internally, reducing query count and speeding up seeding.
<?php use App\Models\User; for ($i = 0; $i < 1000; $i++) { User::factory()->create(); } // Each user triggers separate insert queries without batching or optimization.
| Pattern | Database Queries | Execution Time | Memory Usage | Verdict |
|---|---|---|---|---|
| Single insert per factory instance | 1000 queries for 1000 items | High (seconds) | Moderate | [X] Bad |
| Batch insert with Laravel factory | 1-5 queries for 1000 items | Low (milliseconds) | Low | [OK] Good |
definition to return fake data arrays.definition() matches the method that defines fake fields.User::factory()->make() produce?
public function definition()
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'age' => 25
];
}make() method creates a model instance without saving it to the database, filling fields from definition().public function definition()
{
return [
'title' => fake()->sentence,
'content' => fake()->paragraph(),
];
}fake() returns a Faker instance; methods like sentence() require parentheses to call.fake()->sentence without parentheses accesses a property, which does not exist, causing an error.price is a random number between 10 and 100, but only for products that are 'active'. How should you modify the factory definition to include this condition?active() state method allows setting price randomly and marking product as active only when this state is applied.Pass the test and this pattern moves unknown