Leandro Garcia
Leandro Garcia

Reputation: 3228

Customizing JSON response with additional index keys

I use the response() helper on HTTP Response as documented. The straightforward use:

response()->json(
    $this->response,
    $this->status
)->withHeaders([]);

This will output:

{
    "key" : "desired response"
}

However, I wanted to add a key on the response:

$return['message'] = 'Request successful';
$return['data'] = response()->json(
    $this->response,
    $this->status
)->withHeaders([]);

But the response resulted to:

{
  "message": "Request successful",
  "data": {
    "headers": {},
    "original": {
      "key" : "desired response"
    },
    "exception": null
  }
}

There are extra keys on the response: headers, original & exception. How can I get rid of that in order to achieve this desired format:

 {
    "message": "Request successful",
    "data": {
        "key" : "desired response"
    }
 }

Upvotes: 1

Views: 629

Answers (2)

Mortada Jafar
Mortada Jafar

Reputation: 3679

you can achieve this by using this code:

 $return =[] ;
 $return['message'] = 'Request successful'; // your message
 $return['data'] =  $this->response;  // your data
 return response()->json(     // return json
    $return,  // your data here 
    $this->status    // status here
);

Upvotes: 0

Quynh Nguyen
Quynh Nguyen

Reputation: 3009

You can you Laravel Provider

php artisan make:provider ResponseMacroServiceProvider

<?php

namespace App\Providers;

use Response;
use Illuminate\Support\ServiceProvider;

class ResponseMacroServiceProvider extends ServiceProvider
{

    /**
     * Bootrap the application service
     */
    public function boot() {
        Response::macro('success', function ($headers, $originals) {
            return Response::json([
                'message' => "Request successful",
                'data'   => [
                    'headers' => $headers,
                    'original' => $originals,
                ],
                'error' => [
                    'code' => 0 ,
                    'message' => []
                ]
            ]);
        });


        Response::macro('error', function ($message, $status = 400) {
            if(is_array($message))
                $message_repsonse = $message;
            else
                $message_repsonse = [$message];
            return Response::json([
                'message' => "Request failed",
                'data' => [
                    'headers' => null,
                    'original' => null,
                ]
                'error' => [
                    'code' => $status,
                    'message' => $message_repsonse
                ]
            ]);
        });
    }

    /**
     * Register application service
     * @override
     */
    public function register() {

    }

}

Edit your config/app.php

/*
 * Application Service Providers...
*/
App\Providers\ResponseMacroServiceProvider::class,

And try to handle at your Controller

$headers = 'Your header';
$originals = Your_Model::find(1);

return response()->success($headers, $originals);

return response()->error($validator->errors()->all(), 300);

Upvotes: 1

Related Questions