Lifetime access is $100 off. Ends this week.Join here →
Laravel

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

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 ?

pikepa
pikepa
0
1
325
whoami (Daryl)
whoami (Daryl)

Is this what you mean?

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

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

Beware of the empty/null previous and next post.