Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

blackpearl's avatar

Previous, Next Post

Hello i had a problem with my next and previous buttons.Can someone help me ?

Its my method

public function art($id)
    {
        $post = Post::find($id);

        $previous = Post::where('id', '<', $post->id)->max('id');
        $next = Post::where('id', '>', $post->id)->min('id');

    return view('blog.show')->with('previous', $previous)->with('next', $next);
    }

My view

@extends('master')
@section('title', 'View a post')
@section('content')
    <div class="row">
                <h2>{!! $post->title !!}</h2>
                <img src="{!! asset($post->image) !!}" alt="" class="news-show">
                <p> {!! $post->content !!} </p>
    </div>
    <div class="clearfix"></div>
    <div class="row">
        <<a href="{{ URL::to( 'blog/' . $previous ) }}">Previous</a>
        <a href="{{ URL::to( 'blog/' . $next ) }}">Next</a>
    </div>
@endsection

and Route

Route::get('blog/{id?}','BlogController@art');

Why doesn't work ?

0 likes
14 replies
Prullenbak's avatar

Your links are wrong.

Try

{{ URL::to( 'blog/' . $previous->id ) }}

and

{{ URL::to( 'blog/' . $next->id ) }}

Then, in your controller, you do

$next = Post::where('id', '>', $post->id)->first();

and you get the previous post in a similar way.

By the way, you should also check that there is a next or previous record. You might be viewing the first or last one.

1 like
pmall's avatar

You should name your route and use the route('route.name', [$previous]) helper to generate urls.

Prullenbak's avatar

yes. As @pmaill is saying, something like this would be better:

Route::get('blog/{id}',[
    'as' => 'blog.show',
    'uses' => 'BlogController@art'
]);

Then, in your view, you build your links like this:

<a href="{{ route( 'blog.show', $previous->id ) }}">Previous</a>
pmall's avatar

@Prullenbak quick tip, when you want to generate a route with the id of an object, you can pass the object only : route('show.blog', [$previous]).

Prullenbak's avatar

@pmall Does that also work if you don't make use of route-model binding? If that's the case: nice! Thanks for the tip :)

pmall's avatar

Of course, route model binding is unrelated to url generation. When the url generator see a model as parameter it automagically takes its id. There is a method you can put in the model to return anything else than its id in this case (a slug for example) but I dont remember its name.

Prullenbak's avatar
Level 26

Well, if it's the last post, then

$next = Post::where('id', '>', $post->id)->first();

would be Null. So in your view, you could do

@if($next)
<a href="{{ route( 'blog.show', $next->id ) }}">Next</a>
@endif

Or, as @pmall pointed out:

@if($next)
<a href="{{ route( 'blog.show', $next ) }}">Next</a>
@endif

that's a little bit cleaner.

1 like
dascorp's avatar

Hi, it works however I have couple of questions. what if we:

  • use UUID for id (we cannot compare id for greater or less than)
  • want to reduce amount of DB queries.
phaptq's avatar

Try

$previous = Post::where('id', '<', $post->id)->orderBy('id', 'desc')->first();
$next = Post::where('id', '>', $post->id)->first();
1 like
almokhtar_br's avatar

try this one :

public function single($slug){

    $post = Post::where('slug',$slug)->first();

    $previous_id = Post::where('id','<',$post->id)->max('id');
    $next_id = Post::where('id','>',$post->id)->min('id');

    $next = Post::find($next_id);
    $previous = Post::find($previous_id);


     return view('front-end.single',compact('post',$post))
         ->with('previous',$previous)
         ->with('next',$next);
}

Please or to participate in this conversation.