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

ahoi's avatar
Level 5

"scopeUnverified" returns Call to an undefined method \HasMany::verified() in Larastan

Hello,

I am working with Larastan; at the moment, I am facing a little problem: Larastan throws out the following error:

  137    Call to an undefined method Illuminate\Database\Eloquent\Relations\HasMany::verified().  

Alright, so I had a look at it.

I defined a scope like this:

 /**
     * @param $query
     *
     * @return mixed
     */
    public function scopeVerified($query): mixed
    {
        return $query->where('verified', 1);
    }

This scope is being used like this:

$verified_images = $images->verified()->count();

The relation looks like this:


/**
 * @return HasMany
 */
public function images(): HasMany
{
    return $this->hasMany(Image::class);
}

The code itself works fine - it is doing what it's supposed to do. But how can I get rid of the error in Larastan?

0 likes
7 replies
Snapey's avatar

you have verified and unverified ?

ahoi's avatar
Level 5

@Snapey

Yes. You are right, I did not add all possibilities to this example.

Snapey's avatar

what is $images in

$verified_images = $images->verified()->count();
ahoi's avatar
Level 5

Hi @Snapey,

$images comes from the relation:

/**
 * @return HasMany
 */
public function images(): HasMany
{
    return $this->hasMany(Image::class);
}

And is just


$user->images;

where $user comes from an instance of the current user model.

Snapey's avatar

so $user->images will return a collection. You cannot apply a scope to a collection, only an instance of query builder

ahoi's avatar
Level 5

Solved the issue after a useful hint from the package maintainer.

In the images() relation, it should be a docblock param like this:

    /**
     * @return HasMany<Image>
     */

Please or to participate in this conversation.