LaraBABA's avatar

How to get a chronological id from a collection?

Hello,

I am having a small problem but cannot find a good way to deal with it.

Here is my code:

$datas = User::whereNotIn(
                'role_id', array(1, 2)
            )
                ->whereNotIn
            (
                'total_points', array(0)
            )
                ->orderBy('total_points', 'desc')
                ->paginate(20);

I am pulling 20 records at the time and add them to my pagination

                        <div class="container">
                            <nav aria-label="Page navigation">
                                <ul class="pagination">
                                    {{ $datas->links() }}
                                </ul>
                            </nav>
                        </div>


The problem is that in my results I need to see a incremental count(id) as 1,2,3,4,5,6,7 until the past record. I thought it would have been easy with PHP but my problem is the pagination, in the front end if I create a loop as:
                                if($i < 20) {
                                echo "<div class='sticker-round bg-color-1'>" . ++$i . "</div>";
                                }

It works but as soon as I click on he pagination link, I get the records from 1 to 20 again instead of 21 to 40. I could try to do a GET on the page url to have the pagination number but I find this a bit dirty.

So, perhaps there is a way through eloquent where we can get this extra id while pulling the records, I looked through here:
https://laravel.com/docs/5.7/collections#method-wherenotin

But could not find anything that can help me(unless I missed it).

Any idea if there is a way to add a new property to the object that add this incremental id please?



Thank you.
0 likes
2 replies
bobbybouwmann's avatar
Level 88

Hee! Well yeah, that's true because whenever you go to the next page you refresh the page and $i is 1 again. So instead you need to set $i based on the current page of the pagination. You can for example do something like this in your controller

public function index()
{
    $perPage = 20;

    $users = User::whereNotIn('role_id', [1, 2])
        ->where('total_points', '!=', 0)
        ->orderBy('total_points', 'desc')
        ->paginate($perPage);

    $counter = 1;
    if (!$users->onFirstPage()) {
        // Get the current page (2)
        // Then calculate the current counter
        // currentpage - 1 * number of items + 1

        // For example if on the second page, counter starts with 21
        // (2 - 1) * 20 + 1 = 21 // 2
        // (4 - 1) * 20 + 1 = 61 // page 4
        $counter = ($users->currentPage() - 1) * $users->perPage() + 1;
    }

    return view('users.index', [
        'users' => $users,
        'counter' => $counter,
    ]);
}

In your view you can then use $counter instead of $i.

Let me know if that works for you ;)

Documentation: https://laravel.com/docs/5.7/pagination#paginator-instance-methods

Please or to participate in this conversation.