User.php
public function roles(){ return $this->belongsToMany(Role::class); }
Role.php
public function users(){ return $this->belongsToMany(User::class); }
UserController.php
$user = User::whereHas('roles', function ($q) use ($request) { $q->where('role_id', $request->roles); })
================Request========================
{ "roles":[1,2] }
===============Response=========================
[]
===============Table=============================== role_user
+===+===+
| 1 | 1 |
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 2 |
What should i do to get all the users with exactly two roles? as user 1 has 2 roles in database
whereIn
in your controller query. – Akhtar Munir