Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ssquare's avatar

Missing meta key on api response

My controller:

        $customers = Customer::paginate();
        return new CustomerResource($customers);

My CustomerResource:

    public function toArray($request)
    {
        return parent::toArray($request);
    }

According to docs, it has mentioned that it api will return related data under meta key as

{
    "data": [
			{},{},{}...
    ],
    "links":{
        "first": "http://example.com/pagination?page=1",
        "last": "http://example.com/pagination?page=1",
        "prev": null,
        "next": null
    },
    "meta":{
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "path": "http://example.com/pagination",
        "per_page": 15,
        "to": 10,
        "total": 10
    }
}

But, I am receiving data as:

{
    "current_page": 1,
    "data": [
        {},{},{}

    ],
    "first_page_url": "http://inventory.test/api/customers?page=1",
    "from": 1,
    "last_page": 14,
    "last_page_url": "http://inventory.test/api/customers?page=14",
    "links": [
        {},{}
    ],
    "next_page_url": "http://inventory.test/api/customers?page=2",
    "path": "http://inventory.test/api/customers",
    "per_page": 15,
    "prev_page_url": null,
    "to": 15,
    "total": 200
}

Am I missing something??
0 likes
4 replies
ssquare's avatar

@michaloravec I got the idea here, on this case how could I return searched query as well on the meta.

ssquare's avatar

No, its not working, its returning data in this format

{
    "data": [
        {
            "data": null,
            "links": {
                "self": "link-value"
            }
        },
        {
            "data": null,
            "links": {
                "self": "link-value"
            }
        }
    ],
    "links": {
        "first": "http://inventory.test/api/customers?page=1",
        "last": "http://inventory.test/api/customers?page=10",
        "prev": null,
        "next": "http://inventory.test/api/customers?page=2"
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 10,
        "links": [
            {
                "url": null,
                "label": "« Previous",
                "active": false
            },
            {
                "url": "http://inventory.test/api/customers?page=1",
                "label": "1",
                "active": true
            },
            {
                "url": "http://inventory.test/api/customers?page=2",
                "label": "2",
                "active": false
            },
            {
                "url": "http://inventory.test/api/customers?page=2",
                "label": "Next »",
                "active": false
            }
        ],
        "path": "http://inventory.test/api/customers",
        "per_page": 20,
        "to": 20,
        "total": 200
    }
}

I just want to return search_query under meta tag as a additional key value pair to current meta data.

Please or to participate in this conversation.