簡體   English   中英

我應該如何獲得以下JSON響應?

[英]How should I get the following JSON response?

我正在使用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']);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM