nickstarkloff's avatar

Link to the latest post

I 'm building a forum and I have a list of all the sections. And in these sections are threads. In this list of the sections I want to link to the latest post in a thread.

As on this page, if you click on "Updated X minutes ago".

So the link should look like this..

www.example.com/section/thread?page=1#post-123

Right now I can already link to the thread where the latest post is. But I do not know how I can link it to the page.

?page=1

How I can do that?

Thanks in advance! :)

Edit: So in the thread view I already have a pagination. I only want to link the post to the correct page.

0 likes
3 replies
phildawson's avatar

Like this if you want to add a hash

{!! $foo->fragment('post-123')->render() !!}
nickstarkloff's avatar

The hash is not a problem. Only the page where the post can be seen. (?page=X)

This is how I generate the link at the moment..

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

and this is the lastPost() function..

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();
}

..but this just give me the thread where the last post is... not the page. And that's the question. How can I do that?

pmall's avatar
pmall
Best Answer
Level 56

@SiiXFX get the number of posts in the thread then divide it by the number of posts per page so you have the last page number.

1 like

Please or to participate in this conversation.