首页 > 解决方案 > 打印用户角色 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 安装上创建关系,这些关系已准备好使用

标签: laravellaravel-permission

解决方案


为了得到第一个角色的名字:

$user->roles->first()->name

或者您可以使用逗号分隔所有角色:

$user->roles->implode(', ');

推荐阅读