php - Can you paginate a nested query with Laravel? -
is possible paginate nested query?
user.php
public function favoriteposts() { return $this->morphedbymany('app\post', 'favoritable', 'favorites') ->withtimestamps(); }
profilescontroller.php
public function posts($username) { $user = user::with(['profile', 'favoriteposts' => function($q){ $q->paginate(15); }])->whereusername($username)->firstorfail(); return view('profiles.posts')->withuser($user); }
profiles/posts.blade.php
... {!! $user->favoriteposts->appends(request::except('page'))->render() !!} ...
error
call undefined method illuminate\database\eloquent\collection::appends()
i ended using 2 queries.
public function posts($username) { $user = user::with('profile')->whereusername($username)->firstorfail(); $posts = $user->favoriteposts()->paginate(15); return view('profiles.posts')->withuser($user)->with(compact('posts')); }
Comments
Post a Comment