php - 使用 has Role 返回特定数据
问题描述
我的代码在这里工作正常,但是虽然他在返回中返回用户角色,但我可以阻止用户角色返回它属于使用用户表中的 role_id 的关系
$paths = Path::with(['user','tags'])->where('category_id',1)->get();
foreach($paths as $path){
if($path->user->hasRole('admin')){
$AdminPaths [] = $path;
}
if($path->user->hasRole('user')){
$UserPaths [] = $path;
}
}
return $UserPaths;
我的用户模型
class User extends \TCG\Voyager\Models\User
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'username'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
解决方案
您hasRole
从这里调用函数:
https ://github.com/the-control-group/voyager/blob/1.2/src/Traits/VoyagerUser.php
它执行loadRolesRelations()
加载角色关系,这就是用户加载角色关系的原因。
您可以在检查角色后取消设置角色关系,例如:
foreach($paths as $path){
if($path->user->hasRole('admin')){
$AdminPaths [] = $path;
}
if($path->user->hasRole('user')){
$UserPaths [] = $path;
}
unset($path->user->role);
}
推荐阅读
- asp.net - ASP Web api 在本地机器上工作但不能在生产环境中工作
- css - Angular 5 - Material Snackbar 无法正常工作
- mysql - 当字段值与数字相乘时,MySQL选择错误的结果
- .net - .Net CAD 2014,自定义功能区菜单无法显示
- javascript - 我正在从 API 接收一些文本形式的数据,这些数据在 ReactJS 环境中嵌入了 HTML 标记。我想将该文本转换为 HTML
- visual-studio - 嘿,我有 2 个 [not_required_arguments] 问题 1 在返回 MaterialApp( 另一个在 Scaffold(
- regex - 如何将内容与正则表达式匹配
- android - 如何在 Delphi Firemonkey / Android 应用程序中为 TRectangle 添加触摸效果?
- .net - “obj”文件夹的“.resources”和“.cache”文件是什么?(VB.NET)
- javascript - 包含固定单元格引用的触发器未触发