0
0
Laravelframework~10 mins

Factory definitions in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Factory definitions
Create Factory Class
Define Model & Fields
Set Default Values
Use Factory to Generate Data
Create Model Instances
Done
This flow shows how Laravel factory definitions are created and used to generate model instances with default or customized data.
Execution Sample
Laravel
use Illuminate\Database\Eloquent\Factories\Factory;

class UserFactory extends Factory {
  protected $model = User::class;
  public function definition() {
    return ['name' => fake()->name(), 'email' => fake()->unique()->safeEmail()];
  }
}
Defines a User factory that sets default name and email values for new User instances.
Execution Table
StepActionCode EvaluatedResult
1Create UserFactory classclass UserFactory extends FactoryFactory class ready for User model
2Set model propertyprotected $model = User::class;Factory linked to User model
3Define default datadefinition() returns ['name' => fake()->name(), 'email' => fake()->unique()->safeEmail()]Default attributes set for User
4Call UserFactory::new()->create()Creates a User instance with default dataUser instance saved with generated name and email
5Call UserFactory::new()->make()Creates a User instance without savingUser instance created but not saved to DB
6ExitNo more factory callsFactory usage complete
💡 Factory calls complete, model instances created or made as requested
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
UserFactoryundefinedClass defined with model and definitionInstance created and savedInstance created but not savedFactory usage done
User instancenonenoneUser with name and email savedUser with name and email unsavedUser instances exist as created
Key Moments - 2 Insights
Why does the factory use 'definition()' method to set data?
The 'definition()' method returns default attribute values for the model. Execution table step 3 shows it returns an array of fake data used when creating instances.
What is the difference between 'create()' and 'make()' in factory usage?
'create()' saves the model instance to the database, while 'make()' only creates it in memory without saving. Steps 4 and 5 in the execution table show this difference.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 3?
AThe factory class is linked to the User model
BDefault attribute values are defined for the User model
CA User instance is created and saved
DThe factory class is created
💡 Hint
Refer to the 'Action' and 'Result' columns at step 3 in the execution table
At which step is a User instance created but not saved to the database?
AStep 5
BStep 4
CStep 2
DStep 6
💡 Hint
Check the 'Action' column for 'make()' usage in the execution table
If you want to customize the email when creating a User, which part of the factory would you modify or override?
AThe $model property
BThe create() method call
CThe definition() method
DThe factory class name
💡 Hint
Look at how default data is set in the definition() method in the execution sample
Concept Snapshot
Laravel Factory Definitions:
- Create a factory class extending Factory
- Set protected $model to link model
- Define default attributes in definition() method
- Use create() to save instances, make() to create without saving
- Factories help generate test or seed data easily
Full Transcript
This visual execution trace shows how Laravel factory definitions work. First, a factory class is created and linked to a model using the protected $model property. Then, the definition() method returns default attribute values using faker data. When the factory is used, calling create() generates and saves a model instance with these defaults, while make() creates an instance without saving. Variables track the factory class and model instances through these steps. Key points include understanding the role of definition() for default data and the difference between create() and make(). This helps beginners see how factories automate model instance creation for testing or seeding.