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); })
================请求========================
{ "roles":[1,2] }
===============响应=========================
[]
===============表===============================ROLE_USER
+===+===+
| 1 | 1 |
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 2 |
我应该怎么做才能使所有用户都具有完全两个角色?因为用户1在数据库中有2个角色,所以]
User.php公共函数role(){返回$ this-> belongsToMany(Role :: class); } Role.php公共函数users(){返回$ this-> belongsToMany(User :: class); } UserController.php $ user =用户:: ...
您可以使用whereIn
仅获得具有提供的role_ids
的用户:
您可以计算查询中的角色数量,然后将结果用作属性
您可以尝试这个