2

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"
 }
}
4
  • And what's the issue? what have you tried so far? Commented Aug 2, 2016 at 10:51
  • 2
    The second one is invalid json format. And I don't think so, the first one has any issue in parsing. But you use the format like { "status": "200", "details": { "id": 1, "email": "[email protected]", "mobile": "", "source": "", "source_id": "", "message": "Bad Request : Already Logged In" } } Commented Aug 2, 2016 at 10:51
  • i already get this solution but stay in a problem with android developer he not need something like "details" , "data" and so on . he need a response contain a data directly like { "id" : 1 , "email" : "[email protected] , and so on } i hope you understand me , thank's Commented Aug 2, 2016 at 11:04
  • @EsLaM While not answering directly, let me advertise ResponseBuilder package I created some time ago for Laravel to normalize and simplify wrting APIs with L: github.com/MarcinOrlowski/laravel-api-response-builder Commented Aug 2, 2016 at 12:01

4 Answers 4

9

UPDATE (Feb 9, 2021): If applicable, the best way to serialize your models into JSON is by using Eloquent's API Resources. This is especially useful when you're building RESTful APIs.


Use the response() and json() methods.

$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:

{
    "status": "200",
    "details":  { 
        "id": 1,
        "email": "[email protected]",
        "mobile": "",
        "source": "",
        "source_id": "",
        "message": "Bad Request : Already Logged In"
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is not true at least on Laravel 6.x, just tried: you will get a JSON array, not an object. If you want to have more control, use JsonResource instead.
As of Laravel 10.x, it still returns a JSON array and not an object
2

just return array json

return response()->json(['status'=>200,'data'=>$products]);



"status": 200,
    "data": [
        {
            "id": 15,
            "title": {
                "en": "Someone’s sitting in the shade today because someone planted a tree a long time ago.",
                "ar": "شخص ما يجلس في الظل اليوم لأن أحدهم زرع شجرة منذ زمن بعيد",
                "ca": "Alguien está sentado a la sombra hoy porque alguien plantó un árbol hace mucho tiempo."
            },
            "slug": {
                "en": "someone-s-sitting-in-the-shade-today-because-someone-planted-a-tree-a-long-time-ago",
                "ar": "شخص-ما-يجلس-في-الظل-اليوم-لأن-أحدهم-زرع-شجرة-منذ-زمن-بعيد",
                "ca": "alguien-esta-sentado-a-la-sombra-hoy-porque-alguien-planto-un-arbol-hace-mucho-tiempo"
            },
            "description": {
                "en": "Someone’s sitting in the shade today because someone planted a tree a long time ago.",
                "ar": "شخص ما يجلس في الظل اليوم لأن أحدهم زرع شجرة منذ زمن بعيد",
                "ca": "Alguien está sentado a la sombra hoy porque alguien plantó un árbol hace mucho tiempo."
            },
            "created_at": "2021-03-15T18:31:02.000000Z",
            "updated_at": "2021-03-15T18:31:02.000000Z"
        },
        {
            "id": 14,
            "title": {
                "en": "Define success on your own terms, achieve it by your own rules, and build a life you’re proud to live.",
                "ar": "حدد النجاح بشروطك الخاصة، وتحققه بقواعدك الخاصة، واصنع حياة تفخر بأن تعيشها.",
                "ca": "Defina el éxito en sus propios términos, consígalo según sus propias reglas y construya una vida de la que se sienta orgulloso de vivir."
            },
            "slug": {
                "en": "define-success-on-your-own-terms-achieve-it-by-your-own-rules-and-build-a-life-you-re-proud-to-live",
                "ar": "حدد-النجاح-بشروطك-الخاصة-وتحققه-بقواعدك-الخاصة-واصنع-حياة-تفخر-بأن-تعيشها",
                "ca": "defina-el-exito-en-sus-propios-terminos-consigalo-segun-sus-propias-reglas-y-construya-una-vida-de-la-que-se-sienta-orgulloso-de-vivir"
            },
            "description": {
                "en": "Define success on your own terms, achieve it by your own rules, and build a life you’re proud to live.",
                "ar": "حدد النجاح بشروطك الخاصة، وتحققه بقواعدك الخاصة، واصنع حياة تفخر بأن تعيشها.",
                "ca": "Defina el éxito en sus propios términos, consígalo según sus propias reglas y construya una vida de la que se sienta orgulloso de vivir."
            },
            "created_at": "2021-03-15T18:31:02.000000Z",
            "updated_at": "2021-03-15T18:31:02.000000Z"
        },
        {
            "id": 13,
            "title": {
                "en": "Never give up. Today is hard, tomorrow will be worse, but the day after tomorrow will be sunshine.",
                "ar": "لا تستسلم أبداً اليوم صعب، غدا سيكون أسوأ، لكن بعد غد سيكون شروق الشمس.",
                "ca": "Nunca te rindas. Hoy es duro, mañana será peor, pero pasado mañana habrá sol."
            },
            "slug": {
                "en": "never-give-up-today-is-hard-tomorrow-will-be-worse-but-the-day-after-tomorrow-will-be-sunshine",
                "ar": "لا-تستسلم-أبدا-اليوم-صعب-غدا-سيكون-أسوأ-لكن-بعد-غد-سيكون-شروق-الشمس",
                "ca": "nunca-te-rindas-hoy-es-duro-manana-sera-peor-pero-pasado-manana-habra-sol"
            },
            "description": {
                "en": "Never give up. Today is hard, tomorrow will be worse, but the day after tomorrow will be sunshine.",
                "ar": "لا تستسلم أبداً اليوم صعب، غدا سيكون أسوأ، لكن بعد غد سيكون شروق الشمس.",
                "ca": "Nunca te rindas. Hoy es duro, mañana será peor, pero pasado mañana habrá sol."
            },
            "created_at": "2021-03-15T18:31:02.000000Z",
            "updated_at": "2021-03-15T18:31:02.000000Z"
        }

]

Comments

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

1 Comment

While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
-1

Simply return the array:

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

return $data;

It will be parsed into a JSON response automatically by Laravel:

[..] you may also return arrays. The framework will automatically convert the array into a JSON response

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.