Support the ongoing development of Laravel.io →
Blade Laravel

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

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 &rarr;</a>
			</li>
			<li class="next">
				<a class="btn btn-primary" href="#">&larr; Newer Posts</a>
			</li>
		</ul>
	</div>
@endif

The problem

The default pagination code, of course, does not work for this:

@if($articles->hasPages())
	{!! $articles->withQueryString()->links() !!}
@endif

What is a viable solution for implementing a pager?

Last updated by @ajax30 2 years ago.
0
Solution

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() }}">&larr; Newer Posts</a>
			</li>
			<li class="prev">
				<a class="btn btn-primary {{ $articles->onLastPage() ? 'disabled' : '' }}" href="{{ $articles->nextPageUrl() }}">Older Posts &rarr;</a>
			</li>
		</ul>
	</div>
@endif
0
Solution selected by @ajax30

Sign in to participate in this thread!

Eventy

Your banner here too?

Razvan ajax30 Joined 2 Oct 2021

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.