链接到分页的最新帖子

时间:2015-07-01 08:37:29

标签: php laravel-5

我正在建造一个董事会,我有一份所有部分的清单。在这些部分是线程。在这个部分列表中,我想链接到一个帖子中的最新帖子。

问题是我对帖子有分页。

现在我已经可以链接到最新帖子所在的主题..

// get all Books
list = db.getAllBooks();

并且自定义方法<a href="{{ route('get_thread', [$section->id, App\Test\Helpers::lastPost($section)->thread->id]) }}">Latest Post</a> 是..

lastPost()

但如果帖子是public static function lastPost($section) { $threads = $section->threads; $thread_ids = []; foreach($threads as $thread) { array_push($thread_ids, $thread->id); } $posts = Post::whereIn('thread_id', $thread_ids)->get(); return $posts->last(); } 上的帖子,我不知道如何将其链接到页面。

我怎么能这样做?提前谢谢!

1 个答案:

答案 0 :(得分:0)

好的,我现在就这样做了..

首先我创建了另一种方法..

public static function lastPostPage($thread)
{
    $posts = $thread->posts;

    // The pagination number you've set
    $threadPagination = 6;

    // Count number of posts in thread
    $countPosts = count($posts);

    // Divide the number of posts by the pagination number
    $lastPageNumber = round($countPosts / $threadPagination);

    return "?page=" . $lastPageNumber;
}

然后我更改了模板中的链接..

<a href="{{ route('get_thread', [$section->id, App\Test\Helpers::lastPost($section)->thread->id]) . App\Test\Helpers::lastPostPage(App\Test\Helpers::lastPost($section)->thread) }}">Latest Post</a>

有了这个我得到这样的链接..

http://example.com/board/1/1?page=3

这不是最干净的方式,但它有效!也许还有另一种方式。

laracasts上有人帮助了我。

相关问题