0
0
Laravelframework~10 mins

Factory definitions in Laravel - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aword
Bname
Cname()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' without parentheses
Using incorrect faker method names
Forgetting to call the method
2fill in blank
medium

Complete 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'
AsafeEmail
Bemail
CemailAddress
DrandomEmail
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'email' which is not a faker method
Using 'randomEmail' which does not exist
Forgetting parentheses
3fill in blank
hard

Fix 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'
Atrue
B'true'
C1
D"true"
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
4fill in blank
hard

Fill 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'
Abcrypt
B'password'
C"secret"
Dhash
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)
5fill in blank
hard

Fill 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'
Aname
BsafeEmail
CtoDateTimeString
DrandomElement
Attempts:
3 left
💡 Hint
Common Mistakes
Using randomElement for email
Using now() without formatting
Using incorrect faker methods