1

I am getting results from a database in a controller and storing them as objects in an array

    $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]);

an object would be like

[{"permission_id":1,"name":"foo","object_id":1,"operation_id":1}]   

So then in the view I am trying to get the values and printing them

<b>{{$role}}</b><br>
    @foreach ($permisos as $permiso)
         @foreach ($permiso->name as $name)
            {{$name}}
        @endforeach
    @endforeach

however I am getting Undefined property: Illuminate\Support\Collection::$name

Mntfr
  • 483
  • 6
  • 20
  • just wondering why you wouldn't use relations and eager loading for this? – serdar.sanri Nov 11 '16 at 21:14
  • No real reason its just that its a database that I didnt create so I am going column by column for now – Mntfr Nov 11 '16 at 22:07
  • It looks like you already have relational ids in place in database. I think it would be a lot easier if you create relations in your model and do a eager loading – serdar.sanri Nov 16 '16 at 15:40

1 Answers1

1

Each variable in the array is an array of objects. There's two ways you can go about it.

Add another foreach:

@foreach ($permisos as $permiso)

    @foreach ($permiso as $perm)
        @foreach ($perm->name as $name)
           {{$name}}
        @endforeach
    @endforeach
@endforeach

Or get one full collection with all of the names:

$permissionsName = array_keys($roleSentinel['permissions']);

$permissions = DB::table('sec_permission')->whereIn('name',$permissionsName)->get();
aynber
  • 22,380
  • 8
  • 50
  • 63