try to use latest ()
Mar 21, 2017
7
Level 1
Pagination links() method doesn't exist after sortByDesc on hasMany
While writing a "Top" questions page in my app today I noticed that pagination isn't working ("Method links does not exist. ") when I sort on a hasMany relationship.
/**
* Returns relevant questions sorted by vote according to the tag object
* @param $tags - Tags object returned from get_tags()
* @return mixed
*/
public static function recent_relevant_top($tags) {
// Convert $tags object to array for whereIn()
$tag_array = array();
foreach($tags as $object)
$tag_array[] = $object->name;
$questions = Question::join('tags_questions', 'tags_questions.question_id', '=', 'questions.id')
->join('tags', 'tags.id', '=', 'tags_questions.tag_id')
->select('questions.*')
->whereIn('tags.name', $tag_array)
->orderBy('questions.id', 'desc')
->paginate(5);
$questions = $questions->sortByDesc(function ($questions) {
return $questions->votes->sum('vote');
});
return $questions;
}
The query and pagination works till I get here. Script returns Method links does not exist...
$questions = $questions->sortByDesc(function ($questions) {
return $questions->votes->sum('vote');
});
Why am I losing the ability to paginate when I sortByDesc?
Please or to participate in this conversation.