【发布时间】:2020-01-07 14:28:08
【问题描述】:
我使用 lumen 编写了一个 api,下面是我的代码。
public function customerslist(Request $request)
{
$allTransactions = UserTransaction::where('marchant_id','=',$request->marchant_id)->get();
foreach ($allTransactions as $allTransaction) {
$rows['response']="success";
$rows['message']="Transaction";
$customer_name = MarchantUser::where('id','=',$allTransaction->customer_id)->first();
$response['customer_id'] = $allTransaction->customer_id;
$response['customer_name'] = $customer_name->contact_name;
$response['customer_ph'] = $customer_name->user_mobile_number;
$response['customer_img'] = '';
$response['trans_amount'] = $allTransaction->amount;
$response['transaction_type'] = $allTransaction->transaction_type;
$response['date']=date("d-M-Y:h:m:a",strtotime($allTransaction->created_at));
$transaction['customer_data'][]=$response;
$rows['customer_list']=$transaction;
}
echo json_encode($rows);
}
使用上面的代码,我得到的结果如下:
{
"response": "success",
"message": "Transaction",
"customer_list": {
"customer_data": [
{
"customer_id": "4",
"customer_name": "anjan",
"customer_ph": "8120653256",
"customer_img": "",
"trans_amount": "2000",
"transaction_type": "debit",
"date": "04-Jan-2020:09:01:am"
},
{
"customer_id": "4",
"customer_name": "anjan",
"customer_ph": "8120653256",
"customer_img": "",
"trans_amount": "2000",
"transaction_type": "credit",
"date": "04-Jan-2020:10:01:am"
},
{
"customer_id": "4",
"customer_name": "anjan",
"customer_ph": "8120653256",
"customer_img": "",
"trans_amount": "2000",
"transaction_type": "credit",
"date": "04-Jan-2020:10:01:am"
},
{
"customer_id": "5",
"customer_name": "users",
"customer_ph": "8120653256",
"customer_img": "",
"trans_amount": "4000",
"transaction_type": "debit",
"date": "04-Jan-2020:09:01:am"
},
{
"customer_id": "6",
"customer_name": "Ganesh Ji",
"customer_ph": "8120653250",
"customer_img": "",
"trans_amount": "2000",
"transaction_type": "debit",
"date": "06-Jan-2020:10:01:am"
}
]
}
}
但我的预期输出应该是这样的
{
"response": "success",
"customer_list": [
{
"date": "04-Jan-2020",
"customer_data": [
{
"customer_id": "4",
"customer_name": "Rahul",
"customer_img": "",
"customer_ph": "763783438",
"transaction_amount": "2000",
"transaction_type": "debit"
},
{
"customer_id": "4",
"customer_name": "Anjan",
"customer_img": "",
"customer_ph": "57656765",
"transaction_amount": "2000",
"transaction_type": "advance"
}
]
},
{
"date": "06-01-2020",
"customer_data": [
{
"customer_id": "6",
"customer_name": "Ganesh Ji",
"customer_img": "",
"customer_ph": "763783438",
"transaction_amount": "4000",
"transaction_type": "debit"
}
]
}
客户列表必须根据交易日期有不同的数组对象。我尝试使用 group by 但 group by 在流明中不起作用。如何根据单个日期创建 json 数组对象。
【问题讨论】:
-
这是一个 X-Y 问题。您无法修复 X(流明分组依据),但您认为可以通过执行 Y 来绕过它。但是您也无法执行 Y,现在正在询问如何修复 Y。不要。改为修复 X。