2016-11-17 28 views
1

在我的應用程序中,我對每個帖子都有一個評論部分,每個評論都有一個回覆部分。如何搜索分頁符並查找項目的頁面?

因爲可以有很多評論,我已經創建了兩個paginators:(?頁= ...)?

  • 註釋分頁程序
  • 的答覆分頁程序爲每個評論(comment-(ID) -page = ...)

爲了重定向到特定的評論或通過調用方法回覆,我如何在評論分頁器中找到評論的位置並對答覆執行相同操作?

調用$post->commentPaginator()->currentPage()在視圖內工作,但我如何執行搜索以查找項目的確切頁面?

諸如 $post->getCommentPage($comment)(例如2)的方法將返回評論所在的頁面將是方便的。

+0

我認爲你不能在paginator本身做到這一點。這需要另一個mysql查詢。請參閱http://stackoverflow.com/a/18694483/3551175。你已經有物品數量,所以你只需要在那裏提供第二個查詢。 – Skysplit

回答

-1

假設你用一個時間戳分頁,如created_at,我會嘗試以下操作。

public function getCommentPage($comment, $reply) 
{ 
    // How many items per page 
    $repliesPerPage = 10; 

    // Replies for this comment 
    $replies = $comment->replies()->sortByDesc('created_at'); 

    // Find the rely position by its id 
    $position = array_search($reply->id, array_keys($replies->toArray())); 

    // Get the page position and always round up 
    $page = ceil($position/$repliesPerPage); 

    return page; 
}