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

faramarz91's avatar

problem with model name that have 's'!

Hi everyone. I have many to many user, role relations. so I have good things with $user->roles or $user->roles(). I make pipeline to filter queries like:


$users = app(Pipeline::class)
            ->send(User::query())
            ->through(RoleFilter::class)
            ->thenReturn();

RoleFilter class:


use App\Models\Role as RoleModel;
class Role extends Filter
{
//abstract applyFilter are trigered on Filter handle()
protected function applyFilter(Builder $queryBuilder, $filterData)
{
    $role = RoleModel::where('name', $filterData)->first();
        if (empty($role)){
            return $next($request);
        } else {
            return $queryBuilder->with('roles')
                ->whereHas('roles', function (Builder $query)use($role){
                        $query->where('id', $role->id);
                });
            }
        }

finally, it returns me:

Call to undefined method App\Models\User::role()

Bad Method Call
Did you mean App\Models\User::roles() ?

when I eliminate 's' from the roles method name in User Model and codes wherever I use it, it's done correctly. What's the problem? does whereHas() work with one to one or one to many? works whereHas('roles', function() )correctly in controller. :|

0 likes
4 replies
webrobert's avatar
Level 51

@faramarz91

The name doesn’t match?

You have class Role extends Filter

But I think you mean

class RoleFilter extends Filter

1 like
faramarz91's avatar

@webrobert yes it's my fault to not mention it. yes I make Alise because I have model and Filter named Role

faramarz91's avatar

@webrobert sorry. my bad. my bad. bUt your mention is very helpful to me to change my class name and find my issue. thank you.

Please or to participate in this conversation.