我的应用程序上有索引功能,它显示了一个包含所有用户的表格:
public function index(Request $request)
{
$this->authorize('inet.user.view');
$users = User::with(['roles', 'groups'])->paginate(10);
return view('security.users.index', compact('users'));
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试制作组和角色列表时,我有很多错误。这是表:
<table class="table table-responsive" id="users-table">
<thead>
<th>
@lang('table.generic.name')
</th>
<th>
@lang('table.users.role') & @lang('table.users.group')
</th>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>
{!! $user->fullname !!}
</td>
<td>
{!! ---HERE NEED TO SHOW LIST OF ROLES--- !!}
</td>
</tr>
@endforeach
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
我试图加入implode(', ', theMethod)并获得名称,array_pluck($user, 'roles.name')但不起作用,$user->pluck('roles.name')也没有。
如何在不执行for视图的情况下获取角色和组列表?
只需遍历使用该with()方法加载的嵌套集合:
@foreach($user->roles as $role)
{{ $role->name }}
@endforeach
@foreach($user->groups as $group)
{{ $group->name }}
@endforeach
Run Code Online (Sandbox Code Playgroud)
如果要使用implode(),请使用pluck()获取名称:
implode(', ', $user->roles->pluck('name')->toArray())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2425 次 |
| 最近记录: |