Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kossiecoder's avatar

Laravel 8 factory class - incompatible types

I have warning on phpstorm

Incompatible types: Expected property of type '\App\Models\User', '\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model' provided
private User $user;

protected function setUp(): void
{
    parent::setUp();
    $this->user = User::factory()->create();
}

new factory returns Model instead of User.

Is there a way to return User instead of Model?

Thanks.

0 likes
3 replies
SilenceBringer's avatar

@kossiecoder check your UserFactory. It must be

    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = User::class;

but you have

    protected $model = Model::class;
NielsNumbers's avatar

I have the same issue, I also have

    protected $model = User::class;

in my UserFactory.

It makes sense to me that the Factory returns an Eloquent\Model, as it has no dynamic type-hint (see Illuminate\Database\Eloquent\Factories\Factory).

Only way how to fix it is to use annotations like this:

/** @var User $user */
$this->user = User::factory()->create();

Please or to participate in this conversation.