我应该怎么做才能使所有用户都具有完全两个角色?因为用户1在数据库中有2个角色

问题描述 投票:1回答:3

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 =用户:: ...

php laravel laravel-5.8
3个回答
0
投票

您可以使用whereIn仅获得具有提供的role_ids的用户:


0
投票

您可以计算查询中的角色数量,然后将结果用作属性


0
投票

您可以尝试这个

© www.soinside.com 2019 - 2024. All rights reserved.