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

maulayyacyber's avatar

How to set additional meta Data on Api Resource to top Response

Hi everyone, i have question about Api Resource on Laravel, so i want to set meta data additional on top response.

my controller

public function index()
    {
        $categories = Category::latest()->paginate();

        //return JSON with Resource
        return CategoryResource::collection($categories)
                //add meta on Api Resource
                ->additional([
                    'success' => true,
                    'message' => 'List Data Categories'
                ]);
    }

response

{
    "data": [
        {
            "id": 3,
            "name": "Bencana Alam",
            "slug": "bencana-alam",
            "image": "http://backend-donasi.test/storage/categories/IWDbkYbKHLb6fZZVzPMQoBBYrQ0R9h9DVuRKfAbY.png",
            "created_at": "2021-01-21T07:19:30.000000Z",
            "updated_at": "2021-01-21T07:19:30.000000Z"
        }
    ],
    "links": {
        "first": "http://backend-donasi.test/api/category?page=1",
        "last": "http://backend-donasi.test/api/category?page=1",
        "prev": null,
        "next": null
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "links": [
            {
                "url": null,
                "label": "« Previous",
                "active": false
            },
            {
                "url": "http://backend-donasi.test/api/category?page=1",
                "label": 1,
                "active": true
            },
            {
                "url": null,
                "label": "Next »",
                "active": false
            }
        ],
        "path": "http://backend-donasi.test/api/category",
        "per_page": 15,
        "to": 1,
        "total": 1
    },
    "success": true,
    "message": "List Data Categories"
}

i want return response, like this

{
    "success": true,
    "message": "List Data Categories",
    "data": [
        {
            "id": 3,
            "name": "Bencana Alam",
            "slug": "bencana-alam",
            "image": "http://backend-donasi.test/storage/categories/IWDbkYbKHLb6fZZVzPMQoBBYrQ0R9h9DVuRKfAbY.png",
            "created_at": "2021-01-21T07:19:30.000000Z",
            "updated_at": "2021-01-21T07:19:30.000000Z"
        }
    ],
    "links": {
        "first": "http://backend-donasi.test/api/category?page=1",
        "last": "http://backend-donasi.test/api/category?page=1",
        "prev": null,
        "next": null
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "links": [
            {
                "url": null,
                "label": "« Previous",
                "active": false
            },
            {
                "url": "http://backend-donasi.test/api/category?page=1",
                "label": 1,
                "active": true
            },
            {
                "url": null,
                "label": "Next »",
                "active": false
            }
        ],
        "path": "http://backend-donasi.test/api/category",
        "per_page": 15,
        "to": 1,
        "total": 1
    }
}

any solution for me ?

thanks very much

0 likes
6 replies
fylzero's avatar

@maulayyacyber The collection will just be converted to json on the response so you could possibly just do something like this...

return [
    'collection' => CategoryResource::collection($categories),
    'success' => true,
    ...
];
1 like
fylzero's avatar

Sorry I wasn't paying close enough attention. What about passing the meta data into your resource?

Totally guessing here...

CategoryResource::collection($categories, [
    'success' => true,
    'message' => 'List Data Categories',
]);

Could you do that and pack it as part of the collection? idk

1 like
maulayyacyber's avatar

thanks for help me @fylzero , i try like this

    public function index()
    {
        $categories = Category::latest()->paginate(12);

        //return JSON with Resource
        return CategoryResource::collection($categories, [
            'success' => true,
            'message' => 'List Data Categories',
        ]);
    }

the return response, like this

{
    "data": [
        {
            "id": 3,
            "name": "Bencana Alam",
            "slug": "bencana-alam",
            "image": "http://backend-donasi.test/storage/categories/IWDbkYbKHLb6fZZVzPMQoBBYrQ0R9h9DVuRKfAbY.png",
            "created_at": "2021-01-21T07:19:30.000000Z",
            "updated_at": "2021-01-21T07:19:30.000000Z"
        }
    ],
    "links": {
        "first": "http://backend-donasi.test/api/category?page=1",
        "last": "http://backend-donasi.test/api/category?page=1",
        "prev": null,
        "next": null
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "links": [
            {
                "url": null,
                "label": "« Previous",
                "active": false
            },
            {
                "url": "http://backend-donasi.test/api/category?page=1",
                "label": 1,
                "active": true
            },
            {
                "url": null,
                "label": "Next »",
                "active": false
            }
        ],
        "path": "http://backend-donasi.test/api/category",
        "per_page": 12,
        "to": 1,
        "total": 1
    }
}

the Additional meta cant show here

fylzero's avatar

@maulayyacyber Did you also modify the CategoryResource to accept the array ad add the data to the resource?

1 like
maulayyacyber's avatar

no @fylzero , the CategoryResource still same, like this

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class CategoryResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}

Please or to participate in this conversation.