I am working on a blogging application in Laravel 8.
Un the ArticlesController controller I have:
class ArticlesController extends FrontendController {
public function index() {
// All articles
$articles = Article::paginate(12);
return view('themes/' . $this->theme_directory . '/templates/index',
[
'theme_directory' => $this->theme_directory,
'articles' => $articles
]
);
}
}
The goal is for the posts to have a pager, with links only to the next and previous articles, not a pagination.
<!-- Pager -->
@if($articles->hasPages())
<div class="clearfix">
<ul class="pagination">
<li class="prev">
<a class="btn btn-primary" href="#">Older Posts →</a>
</li>
<li class="next">
<a class="btn btn-primary" href="#">← Newer Posts</a>
</li>
</ul>
</div>
@endif
The default pagination code, of course, does not work for this:
@if($articles->hasPages())
{!! $articles->withQueryString()->links() !!}
@endif
Here is how I solved the problem, using the section of the documentation:
@if($articles->hasPages())
<div class="clearfix">
<ul class="pagination">
<li class="next">
<a class="btn btn-primary {{ $articles->onFirstPage() ? 'disabled' : '' }}" href="{{ $articles->previousPageUrl() }}">← Newer Posts</a>
</li>
<li class="prev">
<a class="btn btn-primary {{ $articles->onLastPage() ? 'disabled' : '' }}" href="{{ $articles->nextPageUrl() }}">Older Posts →</a>
</li>
</ul>
</div>
@endif
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community