nested with function in eloquent

Submitted by sanjok1988 - 4 years ago

case: user has many posts and post has many comments. we need user's posts with comments:

//Eloquent query:

Users::with('posts')->get();

models and relationships:

//In User model:

protected $with="comments"; //it is important

public function posts() {
    $this->hasMany('App\Post');
}

//In Post Model:

public function comments() {
    $this->hasMany('App\Comments');
}