0
votes

I would like to access nested collection attributes with the following structure ["role"=>"user_id"] from $space_roles variable which contains:

 Illuminate\Database\Eloquent\Collection {#3168
     all: [
       App\UserGroup {#3153
         role: "CONTRIBUTORS",
         updated_at: "2019-04-25 09:03:24",
         name: "Contributeurs",
         created_at: "2019-04-25 09:03:24",
         slug: "contributeurs-2",
         id: 765,
         users: Illuminate\Database\Eloquent\Collection {#3152
           all: [],
         },
       },
       App\UserGroup {#3156
         role: "AUTHORS",
         updated_at: "2019-04-25 09:03:24",
         name: "Auteurs",
         created_at: "2019-04-25 09:03:24",
         slug: "auteurs-2",
         id: 1554,
         users: Illuminate\Database\Eloquent\Collection {#3169
           all: [
             App\User {#3180
               updated_at: "2019-04-25 09:01:42",
               created_at: "2019-04-25 09:01:42",
               last_name: "Lisangola",
               company: "Kinshasa Digital",
               first_name: "Christian",
               slug: "christian-lisangola",
               email: "[email protected]",
               id: 1296,
             },
           ],
         },
       },
       App\UserGroup {#3159
         role: "ANIMATORS",
         updated_at: "2019-04-25 09:03:24",
         name: "Animateurs",
         created_at: "2019-04-25 09:03:24",
         slug: "animateurs-2",
         id: 1534,
         users: Illuminate\Database\Eloquent\Collection {#3171
           all: [],
         },
       },
       App\UserGroup {#3162
         role: "ADMINISTRATORS",
         updated_at: "2019-04-25 09:03:24",
         name: "Administrateurs",
         created_at: "2019-04-25 09:03:24",
         slug: "administrateurs-1",
         id: 1323,
         users: Illuminate\Database\Eloquent\Collection {#3172
           all: [
             App\User {#3185
               updated_at: "2019-04-25 09:03:05",
               created_at: "2019-04-25 09:03:05",
               last_name: "Calvin",
               company: "Gravity",
               first_name: "Jean",
               slug: "jean-calvin",
               email: "[email protected]",
               id: 1533,
             },
           ],
         },
       },
     ],
   }

To try get what I want, I've used the laravel collections method pluck by doing $space_roles->pluck('users.id','role') and it returns null for user's attributes.

But I cannot get the expected result.I've tried to do as expained here : Accessing data from a Collection by chaining like this $space_roles->pluck('users')->flatten()->pluck('id'),it works but I only access users and not the role.

I'm stuck on this.

1
Will there ever be more than one user per role? If so, would you want the structure to be ['role' => ['user_ids']]?thisiskelvin
Yes,a user can have multiple rolesChristian Lisangola

1 Answers

3
votes

You can use the map() collection method to return data how you would like:

$space_roles = $space_roles->map(function($role, $key) { 
    return [$role->role => $role->users->pluck('id')->all()];
})->values()->all();

pluck() is used to return all ids from the users.