You'd want to do this within your model, something like this:
public function getPreviousRow()
{
return static::where('id', '<', $this->id)
->orderBy('id', 'desc')
->first();
}
public function getNextRow()
{
return static::where('id', '>', $this->id)
->orderBy('id', 'asc')
->first();
}
public function getPreviousRow()
{
return static::where('id', '<', $this->id)
->orderBy('id', 'desc')
->first();
}
public function getNextRow()
{
return static::where('id', '>', $this->id)
->orderBy('id', 'asc')
->first();
}
If you're using mysql 8 (or postgres) you can also look at the window functions, and probably just join them directly. The functions are LEAD() and LAG().