简体   繁体   English

分页laravel 4集合并按动态生成的变量排序

[英]paginate laravel 4 collection & sort by dynamically-generated variable

Fairly new to the Laravel framework / PHP frameworks in general and decided that I would build a Hacker News clone for my first project. 一般而言,Laravel框架/ PHP框架还算陌生,因此决定为我的第一个项目构建一个Hacker News克隆。 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. 该视图只是一个“ foreach($ stories as $ story)”循环。

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. 在sortBy之前调用paginate返回错误(Paginator类没有sortBy方法),在sortBy之后调用paginate也返回错误。

So how would I paginate the results AND sort by the dynamically-generated score? 那么,如何对结果进行分页并按动态生成的分数进行排序? Thanks. 谢谢。

As the comments suggested, you can sort before it is paginated rather than after (which probably means using SQL to sort it, if you can, instead of sorting after the fact). 正如注释所建议的那样,您可以在分页之前而不是之后进行排序(如果可以的话,这可能意味着使用SQL对其进行排序,而不是在事实之后进行排序)。

Anyway, I'm not sure exactly what fits your use case due to lack of information. 无论如何,由于缺乏信息,我不确定确切适合您的用例。 Here's some ways you can use the Paginator object which might help you. 您可以通过以下几种方式使用Paginator对象 ,这可能会对您有所帮助。

First, you can get the items from the Paginator object: 首先,您可以从Paginator对象获取项目:

$stories = Story::paginate();
$models = $stories->getItems();
// And then sort them...

Second, if you need to sort the items and then re-paginate them , you can do that as well. 其次,如果您需要对项目进行排序然后重新对它们进行分页 ,则也可以这样做。

$stories = Story::all();
$stories->sortBy( ... );

$paginated = Paginator::make($stories, $total, $perpage);

None of my solutions will work exactly for you - for instance the last example here gets all, and then tries to sort them and paginate every story in the database (hardly what you want, defeats the purpose of pagination). 我的解决方案中没有将确切地为你工作-例如上例中在这里得到一切,然后尝试对它们进行排序,并在数据库中(几乎你想要什么,击败分页的目的)分页每一个故事。

Hopefully this gives you some clue on how to use the pagination object if a way that might be useful. 希望这会为您提供一些有关如何使用分页对象的线索,如果有可能的话。

Lastly, what I might suggest doing is making a background process (a command run on a cron job, for instance?) which ranks each article, and stores the ranked results in a cache. 最后,我可能建议做的是做一个后台进程(例如,在cron作业上运行的命令?),该进程对每篇文章进行排名,并将排名结果存储在缓存中。 Then you can grab the story ID's in specific order from the cache and grab those from the database. 然后,您可以按特定顺序从缓存中获取故事ID,并从数据库中获取。 Don't try to load every story in the database, sort them, and then return a subset of them on every page request made by a user. 不要尝试将每个故事加载到数据库中,对其进行排序,然后在用户发出的每个页面请求中返回它们的子集。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM