diff --git a/app/Controllers/TricksController.php b/app/Controllers/TricksController.php index 37e812f..150d060 100644 --- a/app/Controllers/TricksController.php +++ b/app/Controllers/TricksController.php @@ -51,7 +51,10 @@ public function getShow($slug = null) Event::fire('trick.view', $trick); - $this->view('tricks.single', compact('trick')); + $next = $this->tricks->findNextTrick($trick); + $prev = $this->tricks->findPreviousTrick($trick); + + $this->view('tricks.single', compact('trick', 'next', 'prev')); } /** diff --git a/app/Tricks/Repositories/Eloquent/TrickRepository.php b/app/Tricks/Repositories/Eloquent/TrickRepository.php index 3afc2c0..f917c09 100644 --- a/app/Tricks/Repositories/Eloquent/TrickRepository.php +++ b/app/Tricks/Repositories/Eloquent/TrickRepository.php @@ -311,6 +311,38 @@ public function findByTag($slug, $perPage = 9) return [ $tag, $tricks ]; } + /** + * Find the next trick that was added after the given trick. + * + * @param \Tricks\Trick $trick + * @return \Tricks\Trick|null + */ + public function findNextTrick(Trick $trick) + { + $next = $this->model->where('created_at', '>=', $trick->created_at) + ->where('id', '<>', $trick->id) + ->orderBy('created_at', 'asc') + ->first([ 'slug', 'title' ]); + + return $next; + } + + /** + * Find the previous trick added before the given trick. + * + * @param \Tricks\Trick $trick + * @return \Tricks\Trick|null + */ + public function findPreviousTrick(Trick $trick) + { + $prev = $this->model->where('created_at', '<=', $trick->created_at) + ->where('id', '<>', $trick->id) + ->orderBy('created_at', 'desc') + ->first([ 'slug', 'title' ]); + + return $prev; + } + /** * Get the trick creation form service. * diff --git a/app/Tricks/Repositories/TrickRepositoryInterface.php b/app/Tricks/Repositories/TrickRepositoryInterface.php index bf93c1d..1fc98a9 100644 --- a/app/Tricks/Repositories/TrickRepositoryInterface.php +++ b/app/Tricks/Repositories/TrickRepositoryInterface.php @@ -146,4 +146,20 @@ public function incrementViews(Trick $trick); * @return array */ public function findByTag($slug, $perPage = 9); + + /** + * Find the next trick that was added after the given trick. + * + * @param \Tricks\Trick $trick + * @return \Tricks\Trick|null + */ + public function findNextTrick(Trick $trick); + + /** + * Find the previous trick added before the given trick. + * + * @param \Tricks\Trick $trick + * @return \Tricks\Trick|null + */ + public function findPreviousTrick(Trick $trick); } diff --git a/app/views/tricks/single.blade.php b/app/views/tricks/single.blade.php index bb63f4d..36ee996 100644 --- a/app/views/tricks/single.blade.php +++ b/app/views/tricks/single.blade.php @@ -2,130 +2,146 @@ @section('description', $trick->pageDescription) @section('scripts') - - - @if(Auth::check()) - + - @endif + @if(Auth::check()) + + @endif @stop @section('content') -
{{{ $trick->description }}}
-{{{ $trick->code }}}
- {{{ $trick->description }}}
+{{{ $trick->code }}}
+