Home > database >  How to return consistent data types in laravel pagination
How to return consistent data types in laravel pagination

Time:02-01

I have a function that returns staff and there associated attributes as below

foreach ($merchant_user_ac->staffs->sortByDesc('id') as $staff) {
  $wage = Staff::find($staff->id)->totalCommissions($start_date, $end_date);

  $payments = StaffPayment::where('merchant_id', AH::cMiD())
    ->where('user_id', $staff->id)
    ->when(!empty($start_date) && !empty($end_date), function ($q) use (
      $start_date,
      $end_date
    ) {
      $q->whereBetween(\DB::raw('date(payment_date)'), [
        $start_date,
        $end_date,
      ]);
    })
    ->sum('amount');

  $balance_owed = $wage - $payments;

  $transactions->add([
    'id' => $staff->id,
    'name' => $staff->name,
    'profilephoto' => $staff->profilephoto,
    'wage' => $wage,
    'payments' => $payments,
    'salary' => $staff->salary,
    'rent' => $staff->rent,
    'balance_owed' => $balance_owed   $staff->salary - $staff->rent,
  ]);
}

$merchant_staffs = collect(json_decode(json_encode($transactions), false));
$merchant_staffs = $merchant_staffs->paginate(10);
return response()->json($merchant_staffs);

In the results, the first page is OK but the subsequent pages are having a different data type from the first page and displaying the data becomes an issue.

This is the response for the first page

This is the response for the second page

The data key has different data types. I have tried paginating before the foreach loop but the response did not have the pagination links. I have tried adding ->toArray() method when collecting the data but has the same issue of different types.

How can I return the same data in all pagination links?

The data returned is as below

{
"current_page": 1,
"data": [
    {
        "id": 532,
        "name": "George2",
        "profilephoto": "photos/GwSIKoIXUk1GdL7boD7Ht9mSp1loxM1nGcZ5l5Gd.jpg",
        "wage": 0,
        "payments": 10000,
        "salary": "90000.00",
        "rent": "1000.00",
        "balance_owed": 79000
    },
    {
        "id": 528,
        "name": "david",
        "profilephoto": null,
        "wage": 100,
        "payments": 0,
        "salary": "67000.00",
        "rent": "67000.00",
        "balance_owed": 100
    },
    {
        "id": 524,
        "name": "Naggie",
        "profilephoto": null,
        "wage": 0,
        "payments": 0,
        "salary": null,
        "rent": null,
        "balance_owed": 0
    },
    {
        "id": 503,
        "name": "Khaki ",
        "profilephoto": null,
        "wage": 0,
        "payments": 0,
        "salary": null,
        "rent": null,
        "balance_owed": 0
    },
    {
        "id": 502,
        "name": "Susan",
        "profilephoto": null,
        "wage": 0,
        "payments": 0,
        "salary": null,
        "rent": null,
        "balance_owed": 0
    },
    {
        "id": 476,
        "name": "Maggie",
        "profilephoto": null,
        "wage": 0,
        "payments": 17000,
        "salary": null,
        "rent": "15000.00",
        "balance_owed": -32000
    },
    {
        "id": 475,
        "name": "Aggy",
        "profilephoto": null,
        "wage": 0,
        "payments": 15000,
        "salary": "15000.00",
        "rent": null,
        "balance_owed": 0
    },
    {
        "id": 465,
        "name": "Rhoda",
        "profilephoto": null,
        "wage": 0,
        "payments": 0,
        "salary": null,
        "rent": null,
        "balance_owed": 0
    },
    {
        "id": 464,
        "name": "Very New Staff",
        "profilephoto": null,
        "wage": 500,
        "payments": 0,
        "salary": "10000.00",
        "rent": null,
        "balance_owed": 10500
    },
    {
        "id": 422,
        "name": "jane",
        "profilephoto": null,
        "wage": 0,
        "payments": 0,
        "salary": "15000.00",
        "rent": null,
        "balance_owed": 15000
    }
],
"first_page_url": "http://127.0.0.1:8000/api/v1/staff/list?page=1",
"from": 1,
"last_page": 3,
"last_page_url": "http://127.0.0.1:8000/api/v1/staff/list?page=3",
"links": [
    {
        "url": null,
        "label": "« Previous",
        "active": false
    },
    {
        "url": "http://127.0.0.1:8000/api/v1/staff/list?page=1",
        "label": "1",
        "active": true
    },
    {
        "url": "http://127.0.0.1:8000/api/v1/staff/list?page=2",
        "label": "2",
        "active": false
    },
    {
        "url": "http://127.0.0.1:8000/api/v1/staff/list?page=3",
        "label": "3",
        "active": false
    },
    {
        "url": "http://127.0.0.1:8000/api/v1/staff/list?page=2",
        "label": "Next »",
        "active": false
    }
],
"next_page_url": "http://127.0.0.1:8000/api/v1/staff/list?page=2",
"path": "http://127.0.0.1:8000/api/v1/staff/list",
"per_page": 10,
"prev_page_url": null,
"to": 10,
"total": 25
}

Expected data

CodePudding user response:

Try

$pagination = $merchant_user_ac->staffs()->latest()->paginate(10);
$data = $pagination->getCollection()->map(function ($staff) {
    $wage = Staff::find($staff->id)->totalCommissions($start_date, $end_date);

  $payments = StaffPayment::where('merchant_id', AH::cMiD())
    ->where('user_id', $staff->id)
    ->when(!empty($start_date) && !empty($end_date), function ($q) use (
      $start_date,
      $end_date
    ) {
      $q->whereBetween(\DB::raw('date(payment_date)'), [
        $start_date,
        $end_date,
      ]);
    })
    ->sum('amount');

  $balance_owed = $wage - $payments;

  return [
    'id' => $staff->id,
    'name' => $staff->name,
    'profilephoto' => $staff->profilephoto,
    'wage' => $wage,
    'payments' => $payments,
    'salary' => $staff->salary,
    'rent' => $staff->rent,
    'balance_owed' => $balance_owed   $staff->salary - $staff->rent,
  ];
});

return response()->json($pagination->setCollection($data));
}
  • Related