如何在Laravel中返回响应对象json [英] how to return a response object json in laravel

查看:505
本文介绍了如何在Laravel中返回响应对象json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经收到响应,但是我不需要这样

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

{
  "status": "200",
  "id": 1,
  "email": "a@a.a",
  "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": "a@a.a",
  "mobile": "",
  "source": "",
  "source_id": "",
  "message": "Bad Request : Already Logged In"
 }
}

推荐答案

使用response()json()方法.

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

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

这将返回以下JSON:

This will return the following JSON:

{
    "status": "200",
    "details":  { 
        "id": 1,
        "email": "a@a.a",
        "mobile": "",
        "source": "",
        "source_id": "",
        "message": "Bad Request : Already Logged In"
    }
}

这篇关于如何在Laravel中返回响应对象json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆