【问题标题】:How should I get the following JSON response?我应该如何获得以下 JSON 响应?
【发布时间】:2019-06-26 07:28:10
【问题描述】:

我正在使用 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"
            }
        ]
    }

【问题讨论】:

    标签: php json laravel


    【解决方案1】:

    试试这个:

    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']);
        }
    }
    

    【讨论】:

    • 非常感谢我是 laravel 的新手,您宝贵的解决方案节省了我的时间。我需要你的电子邮件 ID。
    • *.com/questions/56784075/… 参考这个问题
    最近更新 更多