weixin_33704591 2014-07-10 16:28 采纳率: 0%
浏览 21

与Laravel分页

I am using a system of infinite scroll pagination with Laravel and Ajax. It works like:

  • I load the first 10 results, simply using Book::orderBy('created_at')->paginate(5);
  • Then, via Ajax I call to another function which must return the next 5 values. But I don't want the first 5 values, only the 5 new ones.

So, I am looking for something like:

    $shown=10;
    $showPerPage=5;
   //The function that I am looking for
    Book::orderBy('created_at')->paginateFrom(10,5)

This would load the five books after the number 10. I've seen Paginator::make(), but I has two problems: first, it requires all the existing values, so I have to ask the db for every result, which is not very efficient, and second, I cannot pass an array to that funcion because Laravel return an object when you use ->get().

Any idea how could I do it?

  • 写回答

1条回答 默认 最新

  • csdn产品小助手 2014-07-10 16:57
    关注

    I think this could solve:

    $shown=10;
    $showPerPage=5;
    Book::orderBy('created_at')->skip($shown)->take($showPerPage)->get();
    

    Basically the skip method adds the OFFSET clause and the take adds the LIMIT one.

    Source: http://laravel.com/docs/queries

    评论

报告相同问题?