model - How to join 3 tables using laravel eloquent -
how can apply query in laravel 5? have 3 table, post, profile, comments
post{id,title,body,user_id} profile{user_id,last_name,first_name} comments{comments,user_id,post_id}
i want kind of structure:
post_id title post last_name first_name
and below comments
comments last_name fist_name
//post model
public function comments(){ return $this->hasmany('app\comments', 'post_id', 'id'); }
//comments model
public function user(){ return $this->belongsto('app\profile', 'user_id'); }
//profile model
public function comments(){ return $this->hasmany('app\comments'); }
and call in controller using:
$posts = posts::with('userprofile','comments', 'comments.user')->get();
then works:) thank , idea :)
Comments
Post a Comment