Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a factory for the User model.
Laravel
use Illuminate\Database\Eloquent\Factories\Factory; class UserFactory extends Factory { protected $model = User::class; public function definition() { return [ 'name' => $this->faker->[1](), ]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' without parentheses
Using incorrect faker method names
Forgetting to call the method
✗ Incorrect
The faker method to generate a name is 'name'. You call it as $this->faker->name().
2fill in blank
mediumComplete the code to generate a random email in the factory definition.
Laravel
'email' => $this->faker->[1](),
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'email' which is not a faker method
Using 'randomEmail' which does not exist
Forgetting parentheses
✗ Incorrect
The faker method 'safeEmail' generates a safe email address for testing.
3fill in blank
hardFix the error in the factory's state method to set the 'is_admin' attribute.
Laravel
public function admin()
{
return $this->state(function (array $attributes) {
return ['is_admin' => [1]];
});
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string 'true' instead of boolean true
Using numeric 1 which may work but is less clear
Using quotes around true
✗ Incorrect
The 'is_admin' attribute should be a boolean true, not a string.
4fill in blank
hardFill both blanks to define a factory with a default password hashed using bcrypt.
Laravel
return [ 'password' => [1]([2]), ];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using hash() instead of bcrypt()
Not quoting the password string
Using double quotes instead of single quotes (both work but be consistent)
✗ Incorrect
Use bcrypt to hash the string 'password' as the default password.
5fill in blank
hardFill all three blanks to create a factory definition that returns a name, email, and a verified email timestamp.
Laravel
return [ 'name' => $this->faker->[1](), 'email' => $this->faker->[2](), 'email_verified_at' => now()->[3](), ];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using randomElement for email
Using now() without formatting
Using incorrect faker methods
✗ Incorrect
Use faker's name() and safeEmail() methods, and convert now() to a datetime string.