简体   繁体   English

Laravel 5.3-雄辩地从特定点获得分页收集的ID?

[英]Laravel 5.3 - Eloquent get paginated collection from certain point with id?

For a blog post, given post_id of 4 which has 1000 comments, I can get comment_id of 400 using eloquent like this: 对于一个博客帖子,给定post_id4 ,它具有1000评论,我可以这样雄辩地得到400 comment_id

$comment = Comment::where('id', 400)->where('post_id', 4)->first();

The question is, how can I get for that same post, the chunk of 10 comments starting with comment_id 400 with the previous 9 comments (for a total of 10). 问题是,对于同一篇文章,我如何获得10条评论的块,从comment_id 400到前9条评论(共10条)。

This is for pagination starting from a certain point in the comments, in this case 400 . 这是为了从注释中的某个点开始分页,在这种情况下为400 If there are any comments before/after 400 for this post, I would need the previous/next pagination urls if they exist. 如果此帖子在400之前/之后有任何评论,则需要前一个/下一个分页网址(如果存在)。

So for example, 400 has more comments around it for this post, so I should get back comments with comment_id 400 - 390 from eloquent, with this pagination (pseudo code): 因此,例如, 400周围有更多的评论对这个职位,所以我应该回来与评论comment_id 400 - 390从雄辩,这个分页(伪代码):

next_page_url: "http://laravel.app?page=61",
prev_page_url: "http://laravel.app?page=59"

What would the eloquent query to handle this situation in laravel be? 在laravel中处理这种情况的雄辩的查询是什么?

I'm not sure why you would do it based on ID - I'd simply push it back 400 spots. 我不确定您为什么要根据ID进行操作-我会将其推回400个位置。 However, here: 但是,这里:

Comment::where('id', '<', 400)->orderBy('id', 'desc')->take(10)->get();

This will get you the 10 previous comments. 这将使您获得之前的10条评论。

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

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