获取带有关系的嵌套json数据Laravel Eloquent模型数组

时间:2017-05-26 13:44:48

标签: mysql database laravel laravel-5 eloquent

我正在使用laravel制作社交网络,我想要展示" '后' '意见' ' comment_by'具有嵌套关系的单个数组中的用户信息

这是我的类和数据库结构

表名字段

  

成员

         ID => primary key,
         name,
         email
  

帖子

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

评论

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

雄辩的模特

  1. Member.php

    类成员扩展了Model { // }

  2. post.php中

    类post扩展Model {     //     public $ timestamps = true;

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

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

    }

  3. 3.Comment.php

    class comment extends Model
      {
    
    
      }
    

    要求获取数组

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

    *预期输出

    {  
       "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":"parthbhatti95@gmail.com",
          "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":"parthbhatti95@gmail.com",
                   "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":"parthbhatti95@gmail.com",
                   "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":"parthbhatti95@gmail.com",
                   "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":"parthbhatti95@gmail.com",
                   "password":"parth123456",
                   "remember_token":"e1b28a30ab467c52924df64034c386d4",
                   "created_at":null,
                   "updated_at":null
                 },
          }
       ]
    }
    

1 个答案:

答案 0 :(得分:0)

根据您的评论,只需将commentedBy关系添加到您的Member模型并急切加载即可。

在评论模型中。

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

然后急切加载关系。

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