Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel previous and next records

People also ask

How to get next and previous record laravel?

Get Next Record in Laravel So you can use the below eloquent query for that: $next_record = Post::where('id', '>', $post->id)->orderBy('id')->first(); As you can see, we have defined the where() , first() , orderBy() likewise first() eloquent queries to get the next posts or records from the database.

How can I get next ID in laravel?

You can get the id of the next record in laravel using the min() method. The code snippets will return the next id which is greater than the specified id in the table.

What is Closure $Next in laravel?

A Closure is an anonymous function. Closures are often used as callback methods and can be used as a parameter in a function. If you take the following example: function handle(Closure $closure) { $closure(); } handle(function(){ echo 'Hello! '; });


Below are your updated controller and view files derived from @ridecar2 link,

Controller:

public function show($id)
{

    // get the current user
    $user = User::find($id);

    // get previous user id
    $previous = User::where('id', '<', $user->id)->max('id');

    // get next user id
    $next = User::where('id', '>', $user->id)->min('id');

    return View::make('users.show')->with('previous', $previous)->with('next', $next);
}

View:

<a href="{{ URL::to( 'users/' . $previous ) }}">Previous</a>
<a href="{{ URL::to( 'users/' . $next ) }}">Next</a>

// in your model file
public function next(){
    // get next user
    return User::where('id', '>', $this->id)->orderBy('id','asc')->first();

}
public  function previous(){
    // get previous  user
    return User::where('id', '<', $this->id)->orderBy('id','desc')->first();

}
// in your controller file
$user = User::find(5); 
// a clean object that can be used anywhere
$user->next();
$user->previous();

I understand the approach being taken here by user2581096 but I am not sure it is efficient (by any standards). We are calling the database 3 times for really no good reason. I suggest an alternative that will be way more efficient and scalable.

Do not pass the previous and next IDs to the view. This eliminates 2 unnecessary database calls.

Create the following routes:

users/{id}/next

users/{id}/previous

These routes should be used in the href attributes of the anchor tags

Add methods in the controller to handle each of the new routes you have created. For example:

 public  function getPrevious(){
        // get previous  user
        $user = User::where('id', '<', $this->id)->orderBy('id','desc')->first();
        return $this->show($user->id);
    }

This function will only be called when you actually click on the button. Therefore, the database call is only made when you need to actually look up the user.


In your App\Models\User.php

...
protected $appends = ['next', 'previous'];

public function getNextAttribute()
{
    return $this->where('id', '>', $this->id)->orderBy('id','asc')->first();
}

public function getPreviousAttribute()
{
    return $this->where('id', '<', $this->id)->orderBy('id','asc')->first();
}

In your Controller you can simply do this:

public function show(User $user)
{
    return View::make('users.show')
    ->with('user', $user)
    ->with('previous', $user->previous)
    ->with('next', $user->next);
}

// yourModel.php
public function previous()
{
   return $this->find(--$this->id);
}

public function next()
{
   return $this->find(++$this->id);
}

Works like magic, you can chain it:

$prevprev = Model::find($id)->previous()->previous();
$nextnext = Model::find($id)->next()->next();

To get next and previous post we can use max and min functions on Model id in laravel. here is an example to get this https://usingphp.com/post/get-next-and-previous-post-link-in-laravel The Controller:

public function post($id)
{
    $post = Post::find($id);
    $previous = Post::where('id', '<', $post->id)->max('id');
    $next = Post::where('id', '>', $post->id)->min('id');
    return view( 'post', compact( 'post', 'next', 'previous' ));
}

The View:

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