0

I'm fetching data to paginate but pagination is not working, the fetch response is added below, if I return the query relation it retrieves correct data but when I pass it to the custom response function it fetch data only but not the pagination links

try {
            $type = 'success';
            $status_code = 200;
            $message = 'Posts data listed.';
            $response = PostResource::collection(Post::with(['associate:id,name,avatar', 'comments:id,commenter_id,commentable_id,comment,created_at'])
                ->latest('posts.created_at')
                ->paginate(2));
        } catch (Exception $e) {
            $type = 'error';
            $status_code = $e->getCode();
            $message = $e->getMessage();
            $response = false;
        }
        return response_data($type, $status_code, $message, $response);

Here is my response_data function code

function response_data($type, $status, $message = false, $response)
{
    return response()->json(['type' => $type, 'status' => $status, 'message' => $message, 'response' => $response]);
}

but if return the query directly it retrieves data with pagination here is my response that retrieved by the collection

{
    "type": "success",
    "status": 200,
    "message": "Posts data listed.",
    "response": [
        {
            "id": 32,
            "associate_id": 5,
            "title": "Test Title",
            "content": "Post descriptuoin",
            "image": "https://oppazz.oppazzgiftcode.com/images/posts/1632472717.Laravel.png",
            "created_at": "2 months ago",
            "associate": {
                "name": "Code Logic Technologies Pvt. Ltd.",
                "avatar": "https://oppazz.oppazzgiftcode.com/images/associates/1633002782_logo.png"
            },
            "comments": [
                {
                    "id": 13,
                    "commenter_id": "62",
                    "commentable_id": "32",
                    "comment": "Nice offer",
                    "created_at": "2 months ago",
                    "user": {
                        "id": 62,
                        "name": "Rikesh Shakya",
                        "username": "rikesh-shakya",
                        "mobile_number": "9823783191",
                        "email": "[email protected]",
                        "provider_id": null,
                        "avatar": "https://oppazz.oppazzgiftcode.com/images/logo.png",
                        "email_verified_at": null,
                        "status": "Y",
                        "created_at": "2021-06-11T18:05:07.000000Z",
                        "updated_at": "2021-06-11T18:05:07.000000Z",
                        "created_by": null,
                        "updated_by": null,
                        "device_type": null
                    }
                },
                {
                    "id": 16,
                    "commenter_id": "88",
                    "commentable_id": "32",
                    "comment": "tetetete",
                    "created_at": "2 months ago",
                    "user": {
                        "id": 88,
                        "name": "Neelam Kera",
                        "username": "neelam-ranjitkar",
                        "mobile_number": "9860322060",
                        "email": "[email protected]",
                        "provider_id": null,
                        "avatar": "https://oppazz.oppazzgiftcode.com/images/logo.png",
                        "email_verified_at": null,
                        "status": "Y",
                        "created_at": "2021-07-15T14:08:21.000000Z",
                        "updated_at": "2021-07-15T14:08:21.000000Z",
                        "created_by": null,
                        "updated_by": null,
                        "device_type": null
                    }
                }
            ],
            "associate_social_sites": {
                "id": 5,
                "associate_id": 5,
                "facebook": "https://www.fachook.com",
                "instagram": "https://www.instagram.com",
                "twitter": "https://www.twitter.com",
                "status": "Y",
                "created_at": null,
                "updated_at": "2021-09-24T09:29:57.000000Z",
                "created_by": null,
                "updated_by": null,
                "device_type": null
            }
        },
        {
            "id": 31,
            "associate_id": 9,
            "title": "OppazZ Coffee For Happiness (Giveaway series)",
            "content": "OppazZ",
            "image": "https://oppazz.oppazzgiftcode.com/images/posts/1632367205.kamloops-art-page-2.jpg",
            "created_at": "2 months ago",
            "associate": {
                "name": "OppazZ",
                "avatar": "https://oppazz.oppazzgiftcode.com/images/associates/1622551849_184399208_2242958835839866_1824735327179728878_n.jpg"
            },
            "comments": [],
            "associate_social_sites": null
        }
    ]
}

how can it be resolved and fetch pagination link along with the data fetched above

5
  • Define how it is not working? Commented Dec 3, 2021 at 8:43
  • only the data are fetching but the pagination links are not showing. Commented Dec 3, 2021 at 8:45
  • which laravel version do you use? Commented Dec 3, 2021 at 8:54
  • i am using laravel version 8 Commented Dec 3, 2021 at 8:56
  • Because you are using the PostResource::collection method, with it you are mutating ( changing) actual data. so if you remove it, or if you modify PostResource class to return also those meta data which one you need. Commented Dec 3, 2021 at 9:10

1 Answer 1

1

The Laravel paginator result classes implement the Illuminate\Contracts\Support\Jsonable interface contract and expose the toJson method, so it's very easy to convert your pagination results to JSON.

You may also convert a paginator instance to JSON by simply returning it from a route or controller action:

https://laravel.com/docs/5.3/pagination#converting-results-to-json

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.