2017-07-15 81 views
1

我已經找到了解決方案,爲數據庫中的下一個和以前的記錄鏈接。但該解決方案只適用於下一個記錄。當我按回鏈接,它顯示第一個記錄。可以幫助我找到一個解決方案來顯示以前的記錄數量最多?Laravel 5.3上一個和下一個記錄鏈接

//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}', '[email protected]'); 
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); 
}); 

回答

0

嘗試使用orderBy()方法和下降方向:

public static function findPrevious($id) 
{ 
    return static::where('id', '<', $id) 
     ->orderBy('id', 'desc') 
     ->first(); 
} 
+0

日Thnx非常much.This工作正常。 – Recep

相關問題