laravel 如何在laravel中返回响应对象json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38718385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:14:22  来源:igfitidea点击:

how to return a response object json in laravel

phpjsonlaravellaravel-5

提问by Es La M

i already get response but i'm not need it like this

我已经得到回应,但我不需要这样

{
  "status": "200",
  "id": 1,
  "email": "[email protected]",
  "mobile": "",
  "source": "",
  "source_id": "",
  "message": "Bad Request : Already Logged In"
}

i'm need return this data in anather object like :

我需要在另一个对象中返回这些数据,例如:

{
 {
  "status": "200",
 }
 { 
  "id": 1,
  "email": "[email protected]",
  "mobile": "",
  "source": "",
  "source_id": "",
  "message": "Bad Request : Already Logged In"
 }
}

回答by Alfonz

Use the response()and json()methods.

使用response()json()方法。

$data = [
    "status" => "200",
    "details" => [
        "id": 1,
        "email": "[email protected]",
        "mobile": "",
        "source": "",
        "source_id": "",
        "message": "Bad Request : Already Logged In"            
    ]
];

return response()->json($data);

This will return the following JSON:

这将返回以下 JSON:

{
    "status": "200",
    "details":  { 
        "id": 1,
        "email": "[email protected]",
        "mobile": "",
        "source": "",
        "source_id": "",
        "message": "Bad Request : Already Logged In"
    }
}

回答by Marvin Collins

Response(['status'=>'data','details'=>'data'], HTTP_OK);