Norway


pagination is quite a simple thing to use, but to determine the amount of pages it makes additional query to the , which may be a problem for amount of . And you can actually avoid it.

Let’s take a simple pagination example from a table with 5000 :

$customers = Customer::paginate();

And then in Blade file you have this:

<table class="table">
    <thead>
    <tr>
        <th>First name</th>
        <th>Last name</th>
        <th>Email</th>
    </tr>
    </thead>
    <tbody>
    @foreach ($customers as $customer)
        <tr>
            <td>{{ $customer->first_name }}</td>
            <td>{{ $customer->last_name }}</td>
            <td>{{ $customer->email }}</td>
        </tr>
    @endforeach
    </tbody>
</table>

{{ $customers->links() }}

Here’s how the queries look in Laravel Debugbar.

Laravel pagination Laravel simplePaginate: more effective with bigger data - Screen Shot 2017 10 24 at 9 - Laravel simplePaginate: more effective with bigger data

As you can see, there’s query for the actual page, and then counting the amount of entries/pages.

What if you don’t need the numbers of pages and you just want to show links for Previous and Next? There’s a function for that called ().

$customers = Customer::simplePaginate(10);

Laravel simplepaginate Laravel simplePaginate: more effective with bigger data - Screen Shot 2017 10 24 at 9 - Laravel simplePaginate: more effective with bigger data

Now, look at the amount of queries.

Hope that helps!

The post Laravel simplePaginate: more effective with bigger data appeared first on Laravel Daily.



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here