mardi 25 juin 2019

How to access properties values in laravel collection

I am getting data from multiple tables using eloquent relationship. here is my code:

 public function allPosts()
    {
        $posts = Post::with('commonContent','postComment','postLike')->get();
        //return $posts;
        foreach($posts as $post){
            echo $post->id;
            echo $post->content_id;

            foreach($post->common_content as $xyz){
              echo $xyz->description;
           }

        }
    }

when I return $posts I get this result:

[
{
id: 2,
user_id: 8,
content_id: 3,
title: null,
created_at: "2019-06-25 10:00:41",
updated_at: "2019-06-25 10:00:41",
common_content: {
id: 3,
description: "this is a post",
media_json: "{"image":"uploads\/j658XUVMuP2dutAgNUTpYvqLABkJLwYXkgX1zTai.png","video":"uploads\/wIsZNWVDZYZYOgdGstNA5dIsexpQAUu4zJo0wp0c.mp4","audio":"uploads\/2AjXxj1NgeUnUlsCkSAgVqgrykb4XwL8sbjin3cC.mpga"}",
created_at: "2019-06-25 10:00:41",
updated_at: "2019-06-25 10:00:41",
media_json_setting: {
image: "uploads/j658XUVMuP2dutAgNUTpYvqLABkJLwYXkgX1zTai.png",
video: "uploads/wIsZNWVDZYZYOgdGstNA5dIsexpQAUu4zJo0wp0c.mp4",
audio: "uploads/2AjXxj1NgeUnUlsCkSAgVqgrykb4XwL8sbjin3cC.mpga"
}
},
post_comment: [ ],
post_like: [ ]
}
]

I am getting error "Invalid argument supplied for foreach()" on this foreach($post->common_content as $xyz). How can I access the properties. Any help would be highly appreciable.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2IKbaXr
via IFTTT

Aucun commentaire:

Enregistrer un commentaire