Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Resource pagination in closures missing metadata #298

Closed
gilbitron opened this issue Aug 31, 2021 · 3 comments · Fixed by #342
Closed

API Resource pagination in closures missing metadata #298

gilbitron opened this issue Aug 31, 2021 · 3 comments · Fixed by #342

Comments

@gilbitron
Copy link

When using pagination with an API Resource inside a closure, the pagination object is missing the metadata. This seems to be a similar issue to #28 but happens specifically when using closures for partial reloads. For example:

return Inertia::render('App/Users', [
    'table' => function () {
        $users = User::paginate(20);

        return [
            'users' => UserResource::collection($users),
        ];
    }
]);

If I don't use a closure or if I don't use the API resource class, the full paginated object (including metadata) is returned in the response.

NickSdot pushed a commit to NickSdot/inertiajs-inertia-laravel that referenced this issue Jan 3, 2022
@NickSdot
Copy link
Contributor

NickSdot commented Jan 3, 2022

Hi. I was able to reproduce this.

Case 1

Not working. ❌ Your case.

return Inertia::render('App/Users', [
    '1test' => function () {
        return [
            'users' => UserResource::collection(User::paginate(5)),
        ];
    },
]);

image

Case 2

Is working. ✅ This is the same approach as in the shared data docs.

return Inertia::render('App/Users', [
    '1test' => [
        'users' => function () {
            return UserResource::collection(User::paginate(5));
        }
    ],
]);

image

Case 3

Is working. ✅ Standard approach.

return Inertia::render('App/Users', [
    '1test' => function () {
        return UserResource::collection(User::paginate(5));
    },
]);

image


@claudiodekker I assume that this is not is an intended behavior? I'll send a PR now and mark it is draft, because I didn't add any new tests. Old still pass. In case it is mergable I am happy to add extra tests fior it.

@gilbitron if you want please test the following PR and let me know if this solves your issue.

@NickSdot NickSdot mentioned this issue Jan 3, 2022
@claudiodekker
Copy link
Member

claudiodekker commented Jan 3, 2022

@NickSdot Thanks for the detailed reproduction 👍

@NickSdot
Copy link
Contributor

NickSdot commented Jan 3, 2022

@NickSdot Thanks for the detailed reproduction 👍

@claudiodekker added test and changes as requested. Ready for review.

claudiodekker pushed a commit that referenced this issue Jan 14, 2022
JasperSpence added a commit to JasperSpence/inertia-laravel that referenced this issue Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Closed 🚪
Development

Successfully merging a pull request may close this issue.

3 participants