Mehdi Alikhani
Mehdi Alikhani

Reputation: 141

How to access to arrays in Laravel MongoDB queries

I have a result from laravel-mongodb query, I don't know how to parse this arrays, enter image description here

as you can see in this collection , in "taggeduser" I have "-storage" array and i just want to get username's inside storage array in every collections, I wrote this code but it's a mess and not working,

        foreach ($tagged as $tags){
            $temp = $tags['taggeduser'];
            foreach ($temp as $key => $value){
              $users[] = $value
            }
        }

returns all values, and i can't access to 'username' field in foreach, enter image description here

Upvotes: 0

Views: 644

Answers (1)

issam abbas
issam abbas

Reputation: 342

Use mapping more info here

https://laravel.com/docs/7.x/collections#method-map

example

$result = $tagged->map(function ($item, $key) {
    return $item['users'] = the username that you need to add here;
});

Upvotes: 1

Related Questions