0

So I want to show post in data table but there is 2 layer of data, the data it look like this

 [
    {
    created_at":
    "id_post":1,
    "name":"trying post",
    "comment_all":
    [
    {
    "id_user":3,
    }
    ],

So I want to get comment_all, I use this code in blade

@foreach($compalls as $compall )
        <tr>
        <td>{{ $compall->coment_all->id_user }}</td>
        </tr>
@endforeach

but I got this error

Property [id_user] does not exist on this collection instance. (View: directory/file.blade.php)

hope you can help me

2 Answers 2

4

Your coment_all is an array. You have to loop through the array to retrieve each id_user:

@foreach ($compalls as $compall)
    <tr>
        @foreach ($compall->coment_all as $coment)
            <td>{{ $coment->id_user }}</td>
        @endforeach
    </tr>
@endforeach
Sign up to request clarification or add additional context in comments.

3 Comments

@AyamGeprek You are not doing it write. It should be $compall->coment_all, not $compalls->coment_all.
@AyamGeprek Take a look at my updated answer. It should be like this. There's two foreach loop.
@AyamGeprek No problem, please mark my answer as the correct one when you can. Have a nice day.
0

You need to loop coment_all because it is an array. like the following:

First foreach $compalls and another foreach inside that to get id_user.

@foreach ($compalls as $compall)
    <tr>
        @foreach ($compall->coment_all as $coment)
            <td>{{ $coment->id_user }}</td>
        @endforeach
    </tr>
@endforeach

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.