❄️ The winter sale is here. 25% off for a limited time. Join us →

Course - Build A Static File Blog with Laravel - Go to Next / Previous


Started this discussion 2 months ago

This was a great course and just what I was looking for to put together a Static site, looking at trying to extend this so that once I am viewing a post, i can move to the next, or previous. Anyone have any ideas ?

whoami (Daryl)

Replied 2 months ago

Is this what you mean?

public function show(Post $post): View
    $previousPost = Post::query()
        ->where('id', '<', $post->id)

    $nextPost = Post::query()
        ->where('id', '>', $post->id)
    return view('posts.show', [
        'post' => $post,
        'previousPost' => $previousPost,
        'nextPost' => $nextPost,

Beware of the empty/null previous and next post.