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
Post a Comment