laravel - 打印用户角色 laravel Spatie
问题描述
我现在正在用用户信息填写表格我也想获得他的角色,我正在使用 Spatie/permission 库
<thead>
<tr>
<th>Name</th>
<th>Last name</th>
<th>E-mail</th>
<th>Role</th>
<th>Status</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@foreach (App\User::all() as $user)
<tr>
<td>{{ __($user->person->first_name)}}</td>
<td>{{ __($user->person->last_name) }}</td>
<td>{{ __($user->email) }}</td>
<td>{{ __() }}</td>
<td>{{ __($user->active) }}</td>
<td><button class="edit btn btn-sm btn-warning" value="{{ __($user->id)}}">Edit</button></td>
<td><button class="delete btn btn-sm btn-danger" value="{{ __($user->id)}}">Delete</button></td>
</tr>
@endforeach
</tbody>
我不知道我是否必须在User
模型或 Spatie 安装上创建关系,这些关系已准备好使用
解决方案
为了得到第一个角色的名字:
$user->roles->first()->name
或者您可以使用逗号分隔所有角色:
$user->roles->implode(', ');
推荐阅读
- google-cloud-platform - 将 GCP 函数设为公开 - IAM 政策更新失败
- java - 抽象类 getter 和 setter 用法
- sql - 两个字符之间的字符串/数字重复多次
- lisp - EVAL:未定义的函数。在 Common LISP 中用作参数
- sql - Oracle SQL - 如何将单个值连接到多值列?
- javascript - 如何将组件(产品)添加到另一个组件(购物车)?
- google-colaboratory - Google colab:YOLOv4-tiny-Darknet-Roboflow 的列车中的问题
- oracle - 如何在 Oracle Apex 应用程序的所有页面中显示一个区域?
- kotlin - 如何创建具有原始列表的每隔一个元素的代理列表?
- java - Appium grid org.openqa.selenium.WebDriverException: 转发新会话时出错找不到