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

jrdavidson's avatar

Composing Eloquent query with orWhere

I'm using Larastan to eliminate some of the static analysis errors I have going on in my codebase. I have several errors involving the the Eloquent Higher Order Messaging Proxy known as orWhere.

I'm using this method as show below.

ModelA::query()->scopeOne()->orWhere->scopeTwo()->get();

After looking through the Larastan repository I stumbled upon this issue that was brought up to fix this issue and the pull request which was merged.

https://github.com/nunomaduro/larastan/issues/885

https://github.com/nunomaduro/larastan/pull/884

I currently have dev-master version of Larastan and have checked to make sure the Builder class as the property-read of orWhere inside of it which it does.

When I run the phpstan on my file that contains the orWhere higher order message proxy it gives me the following error.

23     Call to an undefined method Illuminate\Database\Eloquent\Builder<App\Models\ModelA>::scope2().  

ModelA

<?php

namespace App\Models;

use App\Builders\ModelQueryBuilder;

class ModelA extends ParentModel 
{
    /**
     * Create a new Eloquent query builder for the model.
     *
     * @param  \Illuminate\Database\Query\Builder  $query
     * @return \App\Builders\ModelAQueryBuilder<\App\Models\ModelA>
     */
    public function newEloquentBuilder($query)
    {
        return new ModelABuilder($query);
    }
}
<?php

namespace App\Builders;

/**
 * @template TModelClass of \App\Models\ModelA
 * @extends ParentMemberQueryBuilder<TModelClass>
 */
class ModelAQueryBuilder extends ParentMemberQueryBuilder
{
    /**
     * @return \App\Builders\ModelAQueryBuilder
     */
    public function scopeOne()
    {
        return $this->whateverConstraints();
    }
}
<?php

namespace App\Builders;

/**
 * @template TModelClass of \App\Models\ParentMember
 * @extends GrandparentQueryBuilder<TModelClass>
 */
class ParentMemberQueryBuilder extends GrandparentMemberQueryBuilder
{
}
<?php

namespace App\Builders;

use Illuminate\Database\Eloquent\Builder;

/**
 * @template TModelClass of \App\Models\GrandParent
 * @extends Builder<TModelClass>
 */
class GrandparentMemberQueryBuilder extends Builder
{
    /**
     * @return \App\Builders\GrandparentQueryBuilder
     */
    public function scopeTwo()
    {
        return $this->whereConstraints();
    }
}
0 likes
1 reply
martinbean's avatar

@jrdavidson Again you’re just writing code to satisfy tooling here because you’re not accepting static analysis is doing it’s job and unable to analysis framework “magic” like higher order functions.

If you want things like phpstan to stop complaining about your code, stop using writing code it can’t understand.

1 like

Please or to participate in this conversation.