Level 14
This is almost verbatim the example in the docs for hasManyThrough: http://laravel.com/docs/5.1/eloquent-relationships#has-many-through
I would setup a posts relationship and then grab the 5 most recent as needed
App\User.php
...
protected function posts()
{
return $this->hasManyThrough( 'App\Post', 'App\Blog' );
}
protected function recentPosts( $count )
{
return $this->posts()->latest()->take( $count )->get();
}
...
Then to grab the 5 most recent
Auth::user()->recentPosts(5);
1 like