The Power of @extends in Laravel Factories

πŸ“– 1 minute read

When you generate a new factory in recent Laravel versions, you’ll notice a docblock like this above the class:

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
 */

It’s tempting to ignore it as boilerplate, but it serves a critical purpose for your Developer Experience (DX). This @extends tag tells your IDE (PHPStorm, VS Code) exactly which model this factory is responsible for.

By defining this relationship, your IDE can:

  • Provide auto-complete for model attributes inside the definition() method.
  • Correctly type-hint the return values when calling factory() on the model.
  • Identify missing or misspelled attributes before you even run your tests.

Pro Tip: If you’re working on an older Laravel project that doesn’t have these docblocks, add them manually! It’s a quick way to reduce “red squiggles” and speed up your test writing.

Daryle De Silva

VP of Technology

11+ years building and scaling web applications. Writing about what I learn in the trenches.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *