6

I am making social network using laravel where I want to show " 'post' 'comments' 'comment_by' user info in single array with nested relationship

Here is my class and database structure

table name and Fields

Members

         ID => primary key,
         name,
         email

Posts

         ID => primary key,
         postText
         fromUserId => foreign key (Members-id)

Comments

         commentText ,
         onPostId = > foreign key (Post-id)
         fromUserId = > foreign key (Members-id)

Eloquent Models

  1. Member.php

    class Member extends Model { // }

  2. Post.php

    class post extends Model { // public $timestamps = true;

    function getUserDetails() { return $this->belongsTo('App\Member', 'fromUserId', 'id'); }

    function getCommentDetails() { return $this->hasMany('App\comment', 'onPostId', 'id'); }

    }

3.Comment.php

class comment extends Model
  {


  }

call for getting array

 $posts=post::with('getUserDetails','getCommentDetails')->get();

*expected output

{  
   "id":1,
   "postType":1,
   "postText":"my name is parth",
   "url":null,
   "likesCount":0,
   "unlikesCount":0,
   "shareCount":0,
   "commentsCount":0,
   "thumbUrl":null,
   "accessMode":1,
   "fromUserId":1,
   "isAdult":1,
   "created_at":null,
   "updated_at":null,
   "get_user_details":{  
      "id":1,
      "name":"parth",
      "email":"[email protected]",
      "password":"parth123456",
      "remember_token":"e1b28a30ab467c52924df64034c386d4",
      "created_at":null,
      "updated_at":null
   },
   "get_comment_details":[  
      {  
         "id":1,
         "commentsText":"dccd",
         "onPostId":1,
         "fromUserId":1,
         "created_at":"2017-05-25 16:44:51",
         "updated_at":null
         "commented_by":{  
               "id":1,
               "name":"parth",
               "email":"[email protected]",
               "password":"parth123456",
               "remember_token":"e1b28a30ab467c52924df64034c386d4",
               "created_at":null,
               "updated_at":null
             },
      },
      {  
         "id":3,
         "commentsText":"second comment",
         "onPostId":1,
         "fromUserId":1,
         "created_at":"2017-05-26 09:40:51",
         "updated_at":null
         "commented_by":{  
               "id":1,
               "name":"parth",
               "email":"[email protected]",
               "password":"parth123456",
               "remember_token":"e1b28a30ab467c52924df64034c386d4",
               "created_at":null,
               "updated_at":null
             },
      },
      {  
         "id":4,
         "commentsText":"second comment",
         "onPostId":1,
         "fromUserId":1,
         "created_at":"2017-05-26 09:41:16",
         "updated_at":null
         "commented_by":{  
               "id":1,
               "name":"parth",
               "email":"[email protected]",
               "password":"parth123456",
               "remember_token":"e1b28a30ab467c52924df64034c386d4",
               "created_at":null,
               "updated_at":null
             },
      },
      {  
         "id":5,
         "commentsText":"third one",
         "onPostId":1,
         "fromUserId":1,
         "created_at":"2017-05-26 09:41:43",
         "updated_at":null
         "commented_by":{  
               "id":1,
               "name":"parth",
               "email":"[email protected]",
               "password":"parth123456",
               "remember_token":"e1b28a30ab467c52924df64034c386d4",
               "created_at":null,
               "updated_at":null
             },
      }
   ]
}
3
  • So what exactly is the issue here? Commented May 26, 2017 at 14:30
  • How to get "Commented_by" in comment array Commented May 26, 2017 at 15:42
  • Added my answer. That should do what you need. Commented May 26, 2017 at 16:08

1 Answer 1

3

Based on your comment, just add the commentedBy relationship to your Member model and eager load it.

In Comment model.

public function commentedBy()
{
    return $this->belongsTo('App\Member', 'fromUserId', 'id');
}

Then eager load the relationship like so.

$posts = post::with('getUserDetails','getCommentDetails.commentedBy')->get();
Sign up to request clarification or add additional context in comments.

2 Comments

But how i will call in this $posts=post::with('getUserDetails','getCommentDetails')->get();
@ParthBhatti updated my answer with how to eager load.

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.