Skip to content

Instantly share code, notes, and snippets.

@juliobitencourt
Last active February 9, 2018 19:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliobitencourt/bfd04e590f3fc8daf486 to your computer and use it in GitHub Desktop.
Save juliobitencourt/bfd04e590f3fc8daf486 to your computer and use it in GitHub Desktop.
Simple Previous and Next Navigation with Laravel Eloquent
<?php
namespace App\Domain\Repositories;
use App\Domain\Repositories\DbRepository;
use App\Domain\Entities\SomeEntity;
/**
*
*/
class DbSomeRepository extends DbRepository {
/**
* @var SomeModel
*/
protected $model;
/**
* @var current
*/
protected $current;
/**
* @param SomeModel $model
*/
function __construct(SomeModel $model)
{
$this->model = $model;
}
public function simplePaginate($current)
{
$this->current = $current;
$previous = $this->previous();
$next = $this->next();
return compact('previous', 'next');
}
protected function previous()
{
return $this->simplePaginateQuery('>', 'asc');
}
protected function next()
{
return $this->simplePaginateQuery('<', 'desc');
}
protected function simplePaginateQuery($operator, $order)
{
return $this->model
->where('published_at', $operator, $this->current->published_at)
->orderBy('published_at', $order)
->limit(1)
->first();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment