Laravel getting nested foreach values out of forea

2019-08-21 13:11发布

@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条回答
We Are One
2楼-- · 2019-08-21 14:01

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

查看更多
登录 后发表回答