Skip to content

fix: factory {has,for}* methods should return static#2252

Open
calebdw wants to merge 1 commit into
larastan:3.xfrom
calebdw:static_factory
Open

fix: factory {has,for}* methods should return static#2252
calebdw wants to merge 1 commit into
larastan:3.xfrom
calebdw:static_factory

Conversation

@calebdw
Copy link
Copy Markdown
Contributor

@calebdw calebdw commented Apr 4, 2025

Hello!

The dynamic factory methods {has,for}* (e.g., ->hasUser()) should return static. This especially matters when you are inside the factory itself and are trying to call the methods inside a state:

class CommentFactory extends Factory
{
    public function foo(): static
    {
        return $this
             // this caused the whole chain to return CommentFactory instead of static(CommentFactory)!
            ->forUser()
            ->state(['foo' => 'bar']);
    }
}

This has been released on my fork if anyone would like to take advantage of this immediately.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant