Support Online
Skip to main content

Limiting and Paginating Data Results in Laravel Eloquent Queries

Steps Followed by the User:

  1. Limiting the number of results by replacing the existing all() method in the main route (/) with orderBy() and limit().
  2. Replacing the limit(4)->get() method with simplePaginate(4) in (/{slug}) queries in both the main route (/) and the list route.
  3. Adding the necessary Blade code to add the (resources/views/index.blade.php) pagination links ($links->links()) to the view file.

Brief Technical Summary: This guide explains the use of limit() and simplePaginate() methods to improve performance and readability in Laravel Eloquent queries. Shows how data pulled from the database will be paginated in the frontend (in the Blade template).


💡 What Will You Learn in This Guide?

In this guide, you will learn two basic techniques to reduce load times and improve user experience in Laravel Eloquent queries:

  • Limiting the result set with the limit() method
  • Implementing automatic pagination with the simplePaginate() method

You will also see how these pagination links will be displayed in the frontend (Blade) template.


⚙️ Step 1: Limiting Query Results (limit())

First, let's limit the number of links listed on the application homepage with the limit() method.

Opening the Web Route File: Open the file containing the route definitions.

routes/web.php

Using orderBy()->limit() Instead of all()->sortDesc(): Update the query in the main route (/).

// routes/web.php
Route::get('/', function () {
// En son eklenen 4 bağlantıyı veritabanı seviyesinde çeker
$links = Link::orderBy('created_at', 'desc')->limit(4)->get();

return view('index', [
'links' => $links,
'lists' => LinkList::all()
]);
});

This command sorts and delimits the database instead of pulling all the records and sorting them in PHP.


🔄 Step 2: Paginate the Results (simplePaginate())

Let's use the simplePaginate() method instead of limit() to keep all connections accessible.

Updating Main Route:

Route::get('/', function () {
$links = Link::orderBy('created_at', 'desc')->simplePaginate(4);

return view('index', [
'links' => $links,
'lists' => LinkList::all()
]);
});

This code shows 4 records on each page and creates automatic pagination links.

Updating List Route:

Route::get('/{slug}', function ($slug) {
$list = LinkList::where('slug', $slug)->first();
if (!$list) {
abort(404);
}

return view('index', [
'list' => $list,
'links' => $list->links()->orderBy('created_at', 'desc')->simplePaginate(4),
'lists' => LinkList::all()
]);
})->name('link-list');

Each list page is also limited to 4 records and the user can navigate between pages.


Opening the Blade File:

resources/views/index.blade.php

Adding the Pagination Code:

@if ($links->links()->paginator->hasPages())
<div class="mt-4 p-4 box has-text-centered">
{{ $links->links() }}
</div>
@endif

The $links->links() method automatically creates the pagination navigation.

Checking in Browser: Refresh the page; If there are more than 4 links, you will see “Previous” and “Next” links at the bottom.


❓ Frequently Asked Questions (FAQ)

1. What is the difference between simplePaginate() and paginate()?

simplePaginate() shows forward/reverse connections only. paginate() shows the total pages and the current page number.

2. Why is orderBy() important?

If the order is not specified when paginating, the page order of the records may be confused. That's why orderBy() should always be used at the database level.

3. Does pagination contribute to SEO?

Yes, because the content is broken down into smaller pieces, loading time is reduced and page links are easier to crawl by search engines.

4. How can I make the number of records per page dynamic?

Add a variable like PAGINATION_PER_PAGE=10 to .env and call it via config().


🏁 Result

Thanks to this guide, you have increased query performance and improved user experience in Laravel projects. Now you can easily manage data with limit() and simplePaginate().

💡 Run your projects on GenixNode servers now for greater performance and scalability! ☁️