laravel - How can I search a paginator and find the page of an item? -
in application, have comment section each post , reply section each comment.
since there can many comments, have setup 2 paginators:
- a comment paginator (?page=...)
- a reply paginator each comment (?comment-(id)-page=...)
for purpose of redirecting specific comment or reply calling method, how can find position of comment in comment paginator , same replies?
calling $post->commentpaginator()->currentpage()
works inside views, how can perform search find exact page of item?
a method such as: $post->getcommentpage($comment)
(eg. 2) return page comment located @ convenient.
assuming you're paginating timestamp, created_at
, try following.
public function getcommentpage($comment, $reply) { // how many items per page $repliesperpage = 10; // replies comment $replies = $comment->replies()->sortbydesc('created_at'); // find rely position id $position = array_search($reply->id, array_keys($replies->toarray())); // page position , round $page = ceil($position / $repliesperpage); return page; }
Comments
Post a Comment