2017-05-26 3 views
0

I sozialem Netzwerk Laravel mache, wo ich zeigen will " 'post' 'Kommentare' 'comment_by' user info in einzelnen Array mit verschachtelter BeziehungGet Nested json Array von Daten Laravel Eloquent Modell mit Relationship

hier meine Klasse und Datenbankstruktur

Tabellenname und Felder

Mitglieder

  ID => primary key, 
     name, 
     email 

Beiträge

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

Kommentare

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

Eloquent Modelle

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 
    { 


    } 

Aufruf für Array bekommen

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

* erwartete Ausgabe

{ 
    "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 
      }, 
     } 
    ] 
} 
+0

Also, was genau ist das Problem hier? – Sandeesh

+0

Wie bekomme ich "Commented_by" in Kommentar-Array –

+0

Meine Antwort hinzugefügt. Das sollte tun, was du brauchst. – Sandeesh

Antwort

0

Basierend auf Ihren Kommentar, fügen Sie einfach die commentedBy Beziehung zu Ihrem Member Modell und eifrig Last es.

In Kommentar-Modell.

Dann eifrig laden Sie die Beziehung so.

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

Aber wie ich diese $ posts = post :: with ('getUserDetails', 'getCommentDetails') aufrufen werde -> get(); –

+0

@ParthBhatti aktualisierte meine Antwort mit, wie man eifrig lädt. – Sandeesh

+0

toller Job! das funktioniert –

Verwandte Themen