Created
December 28, 2016 23:24
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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