0
0
Laravelframework~20 mins

Factory definitions in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Factory Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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();
AThe current date and time when the factory is executed
BNull, because the email_verified_at field is not set in the factory
CA random date generated by Faker
DAn empty string
Attempts:
2 left
💡 Hint
Look at the value assigned to 'email_verified_at' in the factory's definition method.
📝 Syntax
intermediate
1: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?
Areturn ['status' => draft];
Breturn ['status' => 'draft']
Creturn ['status' => "draft];
Dreturn ['status' => 'draft'];
Attempts:
2 left
💡 Hint
Check for proper string quoting and syntax completeness.
state_output
advanced
2: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();
AThe code will throw an error because 'admin' is not a valid method
BThe 'is_admin' attribute will be true
CThe 'is_admin' attribute will be null
DThe 'is_admin' attribute will be false
Attempts:
2 left
💡 Hint
Look at how the state method modifies attributes.
🔧 Debug
advanced
2: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,
];
AThe array keys must be quoted strings
BThe faker methods are called incorrectly
CThe inline if-else syntax is invalid in array values
DThe now() function cannot be used inside factory definitions
Attempts:
2 left
💡 Hint
Check the syntax of the conditional expression inside the array.
🧠 Conceptual
expert
2: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?
AIt runs a callback after the model is saved to the database, allowing modifications or related model creation
BIt modifies the model attributes before the model is saved to the database
CIt delays the creation of the model until explicitly called
DIt validates the model attributes before saving
Attempts:
2 left
💡 Hint
Think about when the callback runs in relation to the database save operation.