php - 我应该如何获得以下 JSON 响应?
问题描述
我正在使用 Laravel 5.7 构建一个提供 JSON 响应的 API。我正在创建以下 JSON,但它需要一些更改。我的 JSON 目前不包含表中某些列的一些数组,但根据开发人员的要求,我需要这些数组用于某些列。预期的 JSON 如下。需要解决方案。
控制器:
public function displayAllBookingList(Request $req)
{
$user_id=$req->input('user_id');
$api_token=$req->input('api_token');
$ground_area=$req->input('ground_area');
$query_get_user_details=DB::table('table_registration')
->select('table_registration.*')
->where('user_id',$user_id)
->first();
if($query_get_user_details==NULL)
{
return response()->json(['success' => '0', 'message' =>'Error']);
}
else if($query_get_user_details->api_token==$api_token)
{
$get_ground_list=DB::table('table_ground')
->select('table_ground.*')
->where('ground_area',$ground_area)
->get();
return response()->json(['success' => '1','data' =>$get_ground_list]);
}
else
{
return response()->json(['success' => '0', 'message' =>'Oops Something Wrong']);
}
}
当前 JSON 响应:
{
"success": "1",
"data": [
{
"id": 1,
"ground_id": 1,
"ground_name": "hockey stadium",
"ground_area": "kolhapur",
"booked_status": 0,
"time": "6.00 am to 8.00pm",
"ground_pics": "http://192.168.1.132:8000/images/ground_pic/1.jpg,http://192.168.1.132:8000/images/ground_pic/2.jpg",
"available_sports": "hockey,cricket",
"ground_amenities": "parking,toilet,water",
"ground_rating": 4.5,
"ground_address": "MSEB Ring Road, Datta Colony, Kolhapur, Maharashtra, 416008",
"longitude": "85.501980",
"latitude": "23.624420",
"updated_at": "0000-00-00 00:00:00",
"created_at": "0000-00-00 00:00:00"
}
]
}
所需的 JSON 响应:
{
"success": "1",
"data": [
{
"id": 1,
"ground_id": 1,
"ground_name": "hockey stadium",
"ground_area": "kolhapur",
"booked_status": 0,
"time": "6.00 am to 8.00pm",
"ground_pics": ["http://192.168.1.132:8000/images/ground_pic/1.jpg,http://192.168.1.132:8000/images/ground_pic/2.jpg"],
"available_sports": ["hockey,cricket"],
"ground_amenities": ["parking,toilet,water"],
"ground_rating": 4.5,
"ground_address": [
"MSEB Ring Road, Datta Colony, Kolhapur, Maharashtra, 416008"
],
"longitude": "85.501980",
"latitude": "23.624420",
"updated_at": "0000-00-00 00:00:00",
"created_at": "0000-00-00 00:00:00"
}
]
}
解决方案
尝试这个:
public function displayAllBookingList(Request $req)
{
$user_id = $req->input('user_id');
$api_token = $req->input('api_token');
$ground_area = $req->input('ground_area');
$query_get_user_details = DB::table('table_registration')
->select('table_registration.*')
->where('user_id', $user_id)
->first();
if ($query_get_user_details == NULL) {
return response()->json(['success' => '0', 'message' => 'Error']);
} else if ($query_get_user_details->api_token == $api_token) {
$get_ground_list = DB::table('table_ground')
->select('table_ground.*')
->where('ground_area', $ground_area)
->get();
foreach ($get_ground_list as &$item) {
$ground_pics = [];
foreach (explode(',', $item->ground_pics) as $ground_pic) {
$ground_pics[] = (object)['photo' => $ground_pic];
}
$item->ground_pics = $ground_pics;
$item->available_sports = [$item->available_sports];
$item->ground_amenities = [$item->ground_amenities];
$item->ground_address = [$item->ground_address];
}
return response()->json(['success' => '1', 'data' => $get_ground_list]);
} else {
return response()->json(['success' => '0', 'message' => 'Oops Something Wrong']);
}
}
推荐阅读
- regex - 构建正则表达式
- opencv - CUDA 仅处理 OpenCV 16 位灰度 Mat 中总列的一半
- powershell - New-VM 中的参数-experimental 有什么作用?
- sql-server - SQL Server - 查询以返回不冲突的计划
- javascript - 传单反向地理编码
- javascript - Geoserver : 如何从 geoTIFF 获取真正的 rgb 波段
- javascript - 隐藏 AmCharts 外部数据 URL
- javascript - 遍历嵌套 JSON 对象并添加属性
- javascript - 如何在openlayers 5中右键单击删除功能
- java - 如何在 Java 中绘制形状?