Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用模型结果Laravel进行阵列拾取_Php_Laravel - Fatal编程技术网

Php 使用模型结果Laravel进行阵列拾取

Php 使用模型结果Laravel进行阵列拾取,php,laravel,Php,Laravel,我的应用程序上有索引功能,显示一个包含所有用户的表: public function index(Request $request) { $this->authorize('inet.user.view'); $users = User::with(['roles', 'groups'])->paginate(10); return view('security.users.index', compact('users')); } 但是当我尝试列出组和角色

我的应用程序上有索引功能,显示一个包含所有用户的表:

public function index(Request $request)
{
    $this->authorize('inet.user.view');
    $users = User::with(['roles', 'groups'])->paginate(10);

    return view('security.users.index', compact('users'));
}
但是当我尝试列出组和角色时,我有很多错误。这是一张表:

<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>

@lang('table.generic.name')
@lang('table.users.role')&@lang('table.users.group'))
@foreach($users作为$user)
{!!$user->fullname!!}
{!!---这里需要显示角色列表---!!}
@endforeach
我试图加入
内爆(“,”,theMethod)
并获得名称,但不起作用,也不起作用


如果不在视图中为执行
,如何获取角色和组列表?

只需使用
with()
方法迭代加载的嵌套集合:

@foreach($user->roles as $role)
    {{ $role->name }}
@endforeach

@foreach($user->groups as $group)
    {{ $group->name }}
@endforeach
如果要使用
内爆()

implode(', ', $user->roles->pluck('name')->toArray())

太好了,这就是我想做的。但我刚刚发现一个集合也有内爆方法
$user->roles->implode('name'、'、'))
@Arie很高兴它提供了帮助,感谢您共享
->implode()
解决方案。