Skip to content

Instantly share code, notes, and snippets.

@omurphy27
Created December 28, 2016 23:24
Show Gist options
  • Save omurphy27/873909407b957814b9195dde7a8d876a to your computer and use it in GitHub Desktop.
Save omurphy27/873909407b957814b9195dde7a8d876a to your computer and use it in GitHub Desktop.
Laravel Eloquent Eager Loading - Passing Parameters and Data to With Related Queries and Models
<?php
// must first define the relationships between your models
// see here: https://laravel.com/docs/5.3/eloquent-relationships#eager-loading
$result = Topic::where( 'slug', $slug )->approved()->with([
'comments' => function( $query ) use ( $data ) {
$query->take( $data['limit'] )
->skip( $data['limit'] * ( $data['page'] - 1 ) )
->with('user.profile')
->orderBy('comments.id','desc');
}
])->first();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment