Laravel Testing: Factory States and Relationships
December 24, 2025
•
1 min read
•
60 views
Table of Contents
Advanced factory techniques:
Factory States
public function active(): static
{
return $this->state(fn (array $attributes) => [
'is_active' => true,
]);
}
Create with Relationships
Post::factory()
->has(Comment::factory()->count(3))
->create();
For Relationships
Comment::factory()
->for(Post::factory())
->create();
Factories make testing with complex data structures simple.
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!