Last active
February 9, 2018 19:06
-
-
Save juliobitencourt/bfd04e590f3fc8daf486 to your computer and use it in GitHub Desktop.
Simple Previous and Next Navigation with Laravel Eloquent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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