Laravel getting nested foreach values out of forea

2019-08-21 13:46发布

问题:

@foreach ($getDocs as $key => $docs)
    @foreach ($docs->doc_user as $key1 => $users)
        @foreach ($users->details as $key2 => $value2)

            //Accessing different things here

        @endforeach
    @endforeach
@endforeach

I have form outside of the above foreachs, I need the id of $user like $user->id, I tried hidden field in the above foreachs, And accessed the the value of the that hidden field and gave that value to the hidden field in the form.

That problem with the above method is that the $user->id was the same for every record.

How can i solve this.

回答1:

You can try pluck like this

@foreach ($getDocs as $key => $docs)
    @foreach ($docs->doc_user as $key1 => $users)
        @php
            $user_ids = $users->pluck("id")
        @endphp
        @foreach ($users->details as $key2 => $value2)

            //Accessing different things here

        @endforeach
    @endforeach
@endforeach

and then use $user_ids array