php - Can't load Post with Comment in Laravel -


theese relations:

comment model

class comment extends model {      /**      *      * comment belongs post      *      * @return \illuminate\database\eloquent\relations\belongsto      */     public function post()     {         return $this->belongsto('app\post', 'post_id')->with('post');     }  } 

post model

class post extends model {       /**      *      * post has many comments      *      * @return \illuminate\database\eloquent\relations\hasmany      */     public function comments()     {         return $this->hasmany('app\comment');     } 

then trying check passes:

$comment = comment::findorfail($id);          return response()->json($comment);      } 

only comment stuff retrieved without relation, don't understand, shouldn't eager load post along comment?

while if remove ->with('post'); model , use

$comment = comment::findorfail($id)->load('post'); 

the post gets loaded should work model method anyway.

you should change

$comment = comment::findorfail($id)->load('post'); 

to

$comment = comment::with('post')->findorfail($id); 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -