HDS2019's avatar

Laravel pagination not working in method chain

Works fine:

User::paginate(10)
    ->appends(request()->all());

Output:

{
    "data": [
        {
            "id": 1,
            "email": "[email protected]",
        },
        {
            "id": 2,
            "email": "[email protected]",
        },
],
"current_page": 1,
"first_page_url": "//localhost/users?page=1",
"last_page": 5,
"last_page_url": "//localhost/users?page=5",
"next_page_url": "//localhost/users?page=2",
"path": "//localhost/users",
"per_page": 10,
"prev_page_url": null,
"total": 50
 }

But the problem arrives when I call each() method on it:

Not working

User::paginate(10)
->appends(request()->all())
->each(function ($user) {
    $user['someAttribute'] = 'value';
    return $user;
})

Output (pagination not working) the plain simple result only query records.

[
    {
        "id": 1,
        "email": "[email protected]",
    },
    {
        "id": 2,
        "email": "[email protected]",
    },
]
0 likes
2 replies
MichalOravec's avatar

By the way, fix your github link on Laracasts profile.

1 like

Please or to participate in this conversation.