Laravel Complex Scope Query using chain method

Viewed 93

I want to use user id of whereHas query in the second method. How can I achieve this?

public function scopeloadAvailableAgents($query){
    return $query->whereHas('roles', function($q){
        $q->where("name", "agent");    
    })
    ->whereNotIn('id', function($q) use (/* i want to use user_id of where has query */){
        $q->select('user_id')
            ->from(with(new UserToManager())->getTable())
            ->where('user_id', $user_id);
    })
    ->get();
}

Edit : memeber is a typo.. it will be agent. i have on table agent_to_manager and i want to fetch only agent roled users that is not present in agent_to_manager table.. and i have two queries one that fetch only agent role user and second checks where the fetched users are not present in agent_to_manager table.. but i wont able to get the whereHas query's result to run the second query

1 Answers
  public function scopeloadAvailableAgents($query, $managerId){

        return $query->whereNotIn('id', function($q) use ($managerId){
            $q->select('user_id')
            ->from(with(new UserToManager())->getTable())
            ->where('manager_id', $managerId);
        })
        ->whereHas('roles', function($q){
            $q->where("name", "agent");
            
        })->get();
        
    }
Related