私は掲示板を作っていて、すべてのセクションのリストを持っています。これらのセクションにはスレッドがあります。このセクションのリストでは、スレッドの最新の投稿にリンクしたいと思っています。
問題は、私が投稿のページ付けをしていることです。
今、私はすでに、最新の投稿があるスレッドにリンクすることができます。
<a href="{{ route("get_thread", [$section->id, AppTestHelpers::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();
}
しかし、投稿が例えば上にあれば、私はそれをページにどのようにリンクすることができるのか分かりません ?page=2
..
私はそれをどのようにすることができますか?前もって感謝します!
回答:
回答№1は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, AppTestHelpers::lastPost($section)->thread->id]) . AppTestHelpers::lastPostPage(AppTestHelpers::lastPost($section)->thread) }}">Latest Post</a>
これで、私はこのようなリンクを得る。
http://example.com/board/1/1?page=3
それは一番きれいな方法ではありませんが、それは機能します!別の方法があるかもしれません。
誰かが ララカスト 助けて頂きました。