我正在使用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"
}
]
}
答案 0 :(得分:2)
尝试一下:
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']);
}
}