Fairly new to the Laravel framework / PHP frameworks in general and decided that I would build a Hacker News clone for my first project. However, I've run into a bit of a problem when I try to sort the results by the sorting algorithm and paginate the results.
Controller -
public function index()
{
// tried Story::paginate(10) here instead, results in error
$stories = Story::all();
$stories = $stories->sortBy(function($story)
{
return $story->getScore(); // returns score generated from HN algorithm
})->reverse();
return View::make('stories.index')->with('stories', $stories);
}
The view is just a "foreach($stories as $story)" loop.
The code above works fine, but is not paginated. Calling paginate before sortBy returns an error (Paginator class doesn't have the sortBy method) and calling paginate after sortBy also returns an error.
So how would I paginate the results AND sort by the dynamically-generated score? Thanks.
sortBybefore it becomes apaginatorobject. not after. - itachi