将数据附加到JSON

时间:2017-04-25 07:16:40

标签: json laravel api object laravel-5.4

我在Controller中返回一些json数据作为响应,我想添加一些信息到

在我DriversController的{​​{1}} Apicontroller中我正在DriversController调用返回一些数据,我想将状态代码信息附加到以下回复

api

if ($request->wantsJson()) { return Response::json([ 'data' => [ 'user_details' => $agent_percentage, 'dropdown_data' => [ 'employment_types' => $employment_types->all(), 'roles' => $roles->all(), 'vehicle_brands' => $vehicle_brands->all(), 'vehicle_types' => $vehicle_types->all() ] ] ]); } //to the above response return Response::json([ $this->respondSuccess(), // i am append this information 'data' => [ 'user_details' => $agent_percentage, 'dropdown_data' => [ 'employment_types' => $employment_types->all(), 'roles' => $roles->all(), 'vehicle_brands' => $vehicle_brands->all(), 'vehicle_types' => $vehicle_types->all() ] ] ]); 我正在设置所有状态代码和消息

ApiControllre

但我得到的回应与预期不同

class ApiController extends Controller 
{

    protected $statusCode = 200;

    //getter status code
    public function getStatusCode() 
    {
        return $this->statusCode;
    }

    //setter status code
    public function setStatusCode($statusCode) 
    {
        $this->statusCode = $statusCode;
        return $this;
    }

    //failure messages
    public function respondFailure($message='Account is not active contact admin', $status='failure')
    {
        return $this->setStatusCode(400)->respondWithMessage($message, $status);
    }

    //success messages
    public function respondSuccess($message='Agent is active', $status='success')
    {
        return $this->setStatusCode(200)->respondWithMessage($message, $status);
    }


    //a layer of abstraction to avoide repetation
    public function respond($data, $headers = []) 
    {
        return Response::json($data, $this->getStatusCode(), $headers);
    }

    //get ststus code and message parse it for errors
    public function respondWithMessage($message, $status) 
    {
        return $this->respond([
            'status_code' => $this->getStatusCode(),
            'status' => $status,
            'message' => $message
        ]);
    }

} 

中间件

 //expected result
 {
     "status_code": "200",
     "status": "success",
     "message": "User details with dropdown data",
     "data": {
       "user_details": {
          "id": 2017001,
          "name": "User Name",
          "email": "user@email.com",
        },
        "dropdown_data": {

        }
      }
    }

    //getting response
    {
      "0": {
        "headers": {},
        "original": {
          "status_code": 200,
          "status": "success",
          "message": "Agent is active"
        },
        "exception": null
      },
      "data": {
        "user_details": {
          "id": 2017001,
          "name": "User Name",
          "email": "user@email.com",
        },
        "dropdown_data": {

        }
      }
    }

1 个答案:

答案 0 :(得分:1)

您只是追加respondSuccess()的回复,而不是合并回复。

$this->setStatusCode(200)->respondWithMessage($message, $status);

关于此回复:

return Response::json([
        $this->respondSuccess(), // i am append this information
        'data' => [
            'user_details' => $agent_percentage,
            'dropdown_data' => [
                'employment_types' => $employment_types->all(),
                'roles' => $roles->all(),
                'vehicle_brands' => $vehicle_brands->all(),
                'vehicle_types' => $vehicle_types->all()
            ]
        ]
    ]);

它给出了答案,因为你得不到你期望的答案。

要获得预期的响应,您需要执行以下操作:

public function respondWithMessage($message, $status) 
{
    return [
        'status_code' => $this->getStatusCode(),
        'status' => $status,
        'message' => $message
    ];
}

我只使用了数组而不是$this->respond(),因为你只有消息:

 "status_code": "200",
 "status": "success",
 "message": "User details with dropdown data",

对于您可能需要将两个数组合并为一个的响应类型。 查看array_merge()以获得更多理解。

$responseMessage= $this->respondSuccess();
$data = ['data' => [
        'user_details' => $agent_percentage,
        'dropdown_data' => [
            'employment_types' => $employment_types->all(),
            'roles' => $roles->all(),
            'vehicle_brands' => $vehicle_brands->all(),
            'vehicle_types' => $vehicle_types->all()
        ]
    ]
];

$responseArray = array_merge(responseMessage, data);
return Response::json($responseArray);

我还没有对代码进行测试,但这可能会让您对如何获得所需的预期数组响应有所了解。

如果我错了,任何人都可以建议编辑。