我从控制器中的数据库获取结果并将它们存储为数组中的对象
$roleSentinel = Sentinel::findRoleByName($role);
$permisssionsName = array_keys($roleSentinel['permissions']);
$permissions = array();
foreach ($permisssionsName as $permiso) {
$permissions[] = DB::table('sec_permission')->where('name', '=',$permiso)->get();
}
return view('menu', ['role' => $role,'permisos' => $permisos]);
一个对象就像
[{"permission_id":1,"name":"foo","object_id":1,"operation_id":1}]
那么在视图中我试图获取值并打印它们
<b>{{$role}}</b><br>
@foreach ($permisos as $permiso)
@foreach ($permiso->name as $name)
{{$name}}
@endforeach
@endforeach
然而我得到了Undefined property: Illuminate\Support\Collection::$name
答案 0 :(得分:1)
数组中的每个变量都是一个对象数组。有两种方法可以解决它。
添加另一个foreach:
@foreach ($permisos as $permiso)
@foreach ($permiso as $perm)
@foreach ($perm->name as $name)
{{$name}}
@endforeach
@endforeach
@endforeach
或者获得一个包含所有名称的完整集合:
$permissionsName = array_keys($roleSentinel['permissions']);
$permissions = DB::table('sec_permission')->whereIn('name',$permissionsName)->get();