php - Laravel 5.2 Eager Loading with multiple level one to may relationship -
i'm new laravel , i'm using laravel 5.2 want have comments of particular post of user. tables :
user ----- id public function posts() { $this->hasmany( 'app\posts' ) } post ------ id user_id public function user() { $this->belongsto( 'app\user' ) } public function comments() { $this->hasmany( 'app\comment' ) } comments ---------- id post_id public function post() { $this->belongsto( 'app\post' ) }
now want post perticular user logged in user. can comments long way :
$comments = []; foreach( auth::user()->posts $post) { foreach( $post->comments $comment ) { $comments[] = $comment->title } }
but wondering how comments without making these loops using relations only. in advance.
you can double relationship existing user object:
$posts = \auth::user()->posts()->with('comments')->get();
it should work levels.
in project have organizations clients link table therapists, multiple, , works:
organization::find(1)->clients()->with('therapists')->get();
so case should work too. let me know.
Comments
Post a Comment