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

lara28580's avatar

Query scope "Call to undefined method"

I am trying to build a query scope, but I can't get it to work. I get the following exception "Call to undefined method". The strange thing is the same query directly used on event works. Maybe someone can help me out.

$event->eventParticipantUser($participant)
    public function scopeEventParticipantUser($query, EventParticipant $participant)
    {
        return $query->event_participants()->firstWhere('event_participant_id', $participant->id);
    }
0 likes
1 reply
Nakov's avatar
Nakov
Best Answer
Level 73

The query scope should be used before the eloquent model is being returned, so that's why you are getting that error, because you are trying to access it as a method on the model. The query scope, given the name is being added on the query builder, that's why you need to follow a convention being prefix the method with scope .

this will work

Event::eventParticipantUser($participant)->get(); 
1 like

Please or to participate in this conversation.