Challenge - 5 Problems
Factory Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Laravel factory definition produce?
Consider this Laravel factory definition for a User model. What will be the value of the
email_verified_at attribute when a user is created using this factory?Laravel
<?php use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Str; class UserFactory extends Factory { protected $model = User::class; public function definition() { return [ 'name' => $this->faker->name(), 'email' => $this->faker->unique()->safeEmail(), 'email_verified_at' => now(), 'password' => bcrypt('password'), 'remember_token' => Str::random(10), ]; } } // Usage: // $user = User::factory()->create();
Attempts:
2 left
💡 Hint
Look at the value assigned to 'email_verified_at' in the factory's definition method.
✗ Incorrect
The factory sets 'email_verified_at' to now(), which returns the current date and time when the factory runs, so the created user will have the email verified timestamp set to the current moment.
📝 Syntax
intermediate1:30remaining
Which factory definition syntax is correct for setting a default attribute?
You want to define a Laravel factory for a Post model with a default 'status' attribute set to 'draft'. Which of the following factory definitions is syntactically correct?
Attempts:
2 left
💡 Hint
Check for proper string quoting and syntax completeness.
✗ Incorrect
Option D correctly returns an array with the 'status' key set to the string 'draft' using proper quotes and syntax. Option D misses quotes around 'draft', C has an unclosed string, and D misses the semicolon.
❓ state_output
advanced2:00remaining
What is the output of this factory state modification?
Given this Laravel factory state method, what will be the value of the 'is_admin' attribute when creating a user with
User::factory()->admin()->create();?Laravel
<?php class UserFactory extends Factory { public function definition() { return [ 'name' => $this->faker->name(), 'is_admin' => false, ]; } public function admin() { return $this->state(fn (array $attributes) => [ 'is_admin' => true, ]); } } // Usage: // $user = User::factory()->admin()->create();
Attempts:
2 left
💡 Hint
Look at how the state method modifies attributes.
✗ Incorrect
The 'admin' method uses the state method to override 'is_admin' to true, so when creating a user with admin(), the attribute is true.
🔧 Debug
advanced2:00remaining
Why does this factory definition cause a syntax error?
Examine this factory definition snippet. Why does it cause a syntax error?
Laravel
return [ 'title' => $this->faker->sentence(), 'content' => $this->faker->paragraph(), 'published_at' => $this->faker->boolean() ? now() : null, ];
Attempts:
2 left
💡 Hint
Check the syntax of the conditional expression inside the array.
✗ Incorrect
PHP does not support inline if-else expressions without the ternary operator inside array values. The correct syntax uses the ternary operator: condition ? value_if_true : value_if_false.
🧠 Conceptual
expert2:30remaining
How does Laravel factory's afterCreating callback affect model state?
In Laravel factories, what is the effect of using the
afterCreating callback inside a factory definition?Attempts:
2 left
💡 Hint
Think about when the callback runs in relation to the database save operation.
✗ Incorrect
The afterCreating callback runs after the model is saved, so you can perform actions like creating related models or modifying the saved model.