无法访问 laravel 中的对象 属性
Cant access an object property in laravel
我从控制器中的数据库获取结果并将它们作为对象存储在数组中
$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
数组中的每个变量都是一个对象数组。有两种方法可以解决。
添加另一个 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();
我从控制器中的数据库获取结果并将它们作为对象存储在数组中
$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
数组中的每个变量都是一个对象数组。有两种方法可以解决。
添加另一个 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();