0
votes

I have the below tables

  1. users : id | name
  2. projects : id | name
  3. user_project : id | user_id | project_id

My Models contains

  1. User.php

    public function projects() { return $this->belongsToMany(Project::class); }

  2. Project.php

    public function users() { return $this->belongsToMany(User::class); }

In my controller file, I want to get a list of users using the User model whose role_id is 5.

Something like below

User::query()->roles(5);

*Help will be appreciated

1
Have you omitted something from your question as nothing about it makes a reference to a role model or table.Peppermintology
Why not make it from Role ? Like Role::find(5)->users ?Saromase

1 Answers

2
votes

Have you tried whereHas?

User::whereHas('roles', function($q){
    $q->where('id', 5);
})->get();

This will only return users whose roles.id column equals 5.