Boubou
1 week ago

Having problem accessing data in a collection

Posted 1 week ago by Boubou

Hi all,

I am a real headache with this and cannot work it out.

Let's say you have an eloquent query like this one:

$users = User::select('id', 'email')
        ->with('posts:post_id,post_json')
        ->get();

The result I get is this:

{
"id":9,
"email":"[email protected]",
"posts":
[
{
"post_id":9,
"post_json":"[{\"postsCategories\":{\"1\":true,\"2\":true,\"3\":true,\"4\":true,\"5\":true,\"6\":true,\"7\":true}}]"

}
]},

I would like to access "post_json" per user, but whatever I try, I get stuck after this:

 foreach ($users as $user) {
$user->posts
}

I cannot access the post_id nor post_json

Any idea what am I doing wrong please?

Thank you.

Please sign in or create an account to participate in this conversation.