Laravel Model Factories Advanced
January 16, 2026
•
1 min read
•
18 views
Table of Contents
Advanced factories:
States
public function suspended(): static
{
return $this->state(['status' => 'suspended']);
}
User::factory()->suspended()->create();
Relationships
User::factory()
->has(Post::factory()->count(3))
->create();
Post::factory()
->for(User::factory())
->create();
Sequences
User::factory()
->count(3)
->sequence(
['role' => 'admin'],
['role' => 'user'],
['role' => 'guest'],
)->create();
Callbacks
public function configure()
{
return $this->afterCreating(function (User $user) {
$user->profile()->create();
});
}
Related Posts
Laravel Sanctum API Authentication Complete Guide
Build secure API authentication with Laravel Sanctum for SPAs and mobile apps.
Laravel Rate Limiting: Protect Your Application
Implement rate limiting to protect your Laravel application from abuse.
Laravel Blade Components: Build Reusable UI
Create powerful reusable components with Laravel Blade.
Comments (0)
No comments yet. Be the first to comment!