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

Mahmoud04's avatar

phpstan: Call to an undefined method.

I use LaraStan when i use (with) for specify relationships that should be eager loaded with custom query i get this erros

phpstan: Cannot call method active() on mixed.
phpstan: Cannot call method with() on mixed.

when i try to add return data type like (Illuminate\Database\Eloquent\Relations\Relation) i get this erros

phpstan: Call to an undefined method Illuminate\Database\Eloquent\Relations\Relation::active().

how can i fix this my code:

 public function index(Place $place): Responsable
    {
        return new CollectionResponse(
            key: 'attributes',
            collection: AttributeResource::collection(
                resource: Attribute::query()
                    ->with(['options' => fn (Relation $query) => $query->with('media')->active()])
                    ->whereBelongsTo($place, 'place')
                    ->active()
                    ->get()
            )
        );
    }
0 likes
1 reply
mileswebhosting's avatar

You're running into common PHPStan/Larastan issues., i.e. phpstan: Cannot call method active() on mixed.

This error means PHPStan isn't sure what type $query is inside your closure,

Illuminate\Database\Eloquent\Relations\Relation is a generic base class. PHPStan needs to know the specific relation type (e.g., HasMany, BelongsTo) to correctly infer the available methods, especially custom scopes like active().

Laravel's with() closure receives an instance of the specific relation class (e.g., Illuminate\Database\Eloquent\Relations\HasMany, Illuminate\Database\Eloquent\Relations\BelongsTo, etc.), which then proxies calls to the underlying Eloquent\Builder.

2 likes

Please or to participate in this conversation.