I have found a solution for next and previous records links in database.But the solution is only working good for next record.When i press back link , it shows first record.Can you help me to find a solution for showing biggest number of previous records?
//Model file
public static function findNext($id)
{
return static::where('id','>',$id)->first();
}
public static function findPrevious($id)
{
return static::where('id','<',$id)->first();
}
//Controller file
public function show($id)
{
$c = C::findOrFail($id);
$nextUser = C::findNext($id);
$previousUser = C::findPrevious($id);
return view('c.show', compact('c',$c))->with('nextUser',$nextUser)-
>with('previousUser',$previousUser);
}
//View file
@if($previousUser)
<a href="{{ URL::to( 'c/show/' . $previousUser->id . '/previous')
}}">Previous</a> |
@endif
@if($nextUser)
<a href="{{ URL::to( 'c/show/' . $nextUser->id . '/next') }}">Next</a>
@endif
//Route file
Route::get('c/show/{id}', 'CController@show');
Route::get('c/show/{user}/next',function($id){
$nextUser=C::findNext($id);
return Redirect::to('c/show/'.$id);
});
Route::get('c/show/{user}/previous',function($id){
$nextUser=C::findPrevious($id);
return Redirect::to('c/show/'.$id);
});