Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel add custom attribute to paginate result

I'm trying to add some data into the paginate result. This is the code:

// query select and filter
[...]

$totalAmount = $sales->sum('amount');
$result = $sales->paginate($rowsPerPage);
return $result;

The result is:

{
    "datatable": {
        "current_page": 1,
        "data": [
            {
                "sale": 13047689,
                "customer": "0017850000104",
                "date": "2015-12-23 23:15:06",
                "amount": "129.84",
            }
        ],
        "first_page_url": "http://intranet/api/user/sales/datatable?custom=value&page=1",
        "from": 1,
        "last_page": 1,
        "last_page_url": "http://intranet/api/user/sales/datatable?custom=value&page=1",
        "next_page_url": null,
        "path": "http://intranet/api/user/sales/datatable",
        "per_page": "10",
        "prev_page_url": null,
        "to": 1,
        "total": 1
    }
}

I'd like to add the totalAmount into this paginate object.

What I tried:

$result->appends('totalAmount', $totalAmount);

I find this method over internet but seems it does nothing... The result is exactly like the above without this attribute.

Another method I found is put.

$result->put('totalAmount', $totalAmount);

This method really works but not the way I need. This is for add some rows to the result because it adds the attribute inside "data". This breaks my code when I pass this results for datatable.

{
    "datatable": {
        "current_page": 1,
        "data": {
            "0": {
                "sale": 13047689,
                "customer": "0017850000104",
                "date": "2015-12-23 23:15:06",
                "amount": "129.84",
            },
            "totalAmount": "129.84"
        },
        "first_page_url": "http://intranet/api/user/sales/datatable?page=1",
        "from": 1,
        "last_page": 1,
        "last_page_url": "http://intranet/api/user/sales/datatable?page=1",
        "next_page_url": null,
        "path": "http://intranet/api/user/sales/datatable",
        "per_page": "10",
        "prev_page_url": null,
        "to": 2,
        "total": 1
    }
}

How can I do it???

I'd like this:

{
    "datatable": {
        "current_page": 1,
        "data": [
            {
                "sale": 13047689,
                "customer": "0017850000104",
                "date": "2015-12-23 23:15:06",
                "amount": "129.84",
            }
        ],
        "first_page_url": "http://intranet/api/user/sales/datatable?page=1",
        "from": 1,
        "last_page": 1,
        "last_page_url": "http://intranet/api/user/sales/datatable?page=1",
        "next_page_url": null,
        "path": "http://intranet/api/user/sales/datatable",
        "per_page": "10",
        "prev_page_url": null,
        "to": 2,
        "total": 1,
        "totalAmount": "129.84"
    }
}

Or the further step would be:

{
    "datatable": {
        "current_page": 1,
        "data": [
            {
                "sale": 13047689,
                "customer": "0017850000104",
                "date": "2015-12-23 23:15:06",
                "amount": "129.84",
            }
        ],
        "first_page_url": "http://intranet/api/user/sales/datatable?page=1",
        "from": 1,
        "last_page": 1,
        "last_page_url": "http://intranet/api/user/sales/datatable?page=1",
        "next_page_url": null,
        "path": "http://intranet/api/user/sales/datatable",
        "per_page": "10",
        "prev_page_url": null,
        "to": 2,
        "total": 1,
        "totalData": {
            "accumulated1": 235,
            "accumulated2": 799,
            "accumulated3": 1680,
        },
    }
}

Any help? Thanks.

like image 616
Marc Pont Avatar asked Sep 19 '25 04:09

Marc Pont


1 Answers

you need to create resource for your response first, then add meta data to result like this:

return (new UserCollection(User::all()->load('roles')))
                ->additional(['meta' => [
                    'key' => 'value',
                ]]);

check this out

like image 199
Mohsen Safari Avatar answered Sep 20 '25 18:09

Mohsen Safari