[英]How to format json array object in lumen?
I have written an api using lumen below is my code.我已经使用下面的 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);
}
with the above code I am getting result as follows:使用上面的代码,我得到的结果如下:
{
"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"
}
]
}
}
But my expected output should look like但我的预期输出应该是这样的
{
"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"
}
]
}
Customer list must have different array object based on transaction date.客户列表必须根据交易日期有不同的数组对象。 I tried using group by but group by is not working in lumen.
我尝试使用 group by 但 group by 在流明中不起作用。 How to create a json array object based on individual date.
如何基于单个日期创建一个 json 数组对象。
change $transaction['customer_data'][]
to $transaction[]['customer_data']
.将
$transaction['customer_data'][]
更改$transaction[]['customer_data']
。 but if you want to make date
like that you should make merge array first.但是如果你想这样制作
date
,你应该先制作合并数组。 below is full code:以下是完整代码:
public function customerslist(Request $request)
{
$allTransactions = UserTransaction::where('marchant_id', '=', $request->marchant_id)->get();
foreach ($allTransactions as $allTransaction) {
$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;
$temp['date'] = date("d-M-Y:h:m:a",
strtotime($allTransaction->created_at));
$temp["customer_data"] = $response;
$transaction[] = $temp;
}
$rows['response'] = "success";
$rows['message'] = "Transaction";
$rows['customer_list'] = $transaction;
return $rows;
}
btw $rows
should be placed in outside foreach because only need to set once, and without json_encode it's automatically encoded if you use return btw
$rows
应该放在 foreach 外面,因为只需要设置一次,如果没有 json_encode,它会自动编码,如果你使用 return
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.