jeudi 25 avril 2019

Cannot access data from nested Laravel collection

I would like to access nested collection attributes with the following structure ["role"=>"user_id"] from $sapce_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: "christian@kinshasadigital.com",
               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: "jean@gmail.com",
               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 work but i only access users and not the role.I'm stuck on this.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire