How to load a nested relationship in Laravel How to load a nested relationship in Laravel laravel laravel

How to load a nested relationship in Laravel


You can do this with:

$modelResults = PublicCallback::with(['loan', 'loan.applicant'])->get();

Ref: https://laravel.com/docs/5.5/eloquent-relationships#eager-loading


Just for posterity, there's also another way of loading nested relationships that can be done against a returned model, provided you have set up the relationships correctly:

Posts model:

public function comments() {    return $this->hasMany('App\Comment', 'quote_id', 'id');}

Comments model:

public function user() {    return $this->belongsTo('App\User');}

Then you can actually infer the user via relationship to a comment by drawing the post but loading an array of relations, eg:

$post = \App\Post::find($post_id);return $post->load(['comments','comments.user']);