如何在laravel分页中返回一致的数据类型

umuewwlo  于 2023-01-31  发布在  其他
关注(0)|答案(1)|浏览(107)

我有一个函数,它返回staff和相关属性,如下所示

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);

在结果中,第一页正常,但后续页的数据类型与第一页不同,显示数据成为一个问题。

数据键具有不同的数据类型。我已尝试在foreach循环之前分页,但响应没有分页链接。我已尝试在收集数据时添加->toArray()方法,但存在不同类型的相同问题。
如何在所有分页链接中返回相同的数据?
返回的数据如下

{
"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
}

r8uurelv

r8uurelv1#

试试看

$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));
}

相关问题