Eloquentを使ってfindの前後を取得する
Eloquentを使ってfindの前後を取得するテクニックがあった。
How to get previous / next records in DB with Eloquent. (Page 1) / Laravel 3.x Code Samples / Laravel Forums
なるほど、モデルにそういうメソッドを追加すればいいのか。
idだったら上記のテクニックでOKだけど、id以外の要素だと工夫が必要だね。
public function get_next() { return static::where('votes', '<', $this->votes) ->where('subject', $this->subject) ->orderBy('votes','DESC') ->first(); } public function get_previous() { return static::where('votes', '>', $this->votes) ->where('subject', $this->subject) ->orderBy('votes','DESC') ->first(); }