Wesley Schravendijk
Wesley Schravendijk

Reputation: 495

Laravel Resource Output

Quick question, With Laravel I want to create a json output. For this I use the laravel resource,

The standard resource looks like this

return [
    'id'                  =>      'test',
    'first'               =>      'test',
    'last'                =>      'test',
];

is there also a way to make the resource looks like something like this? what would be the best way to accomplish this.

return [
    'common' => [
        'status' => 'succes',
        'message' => 'succes',
    ],
    'data' => [
        'm_users' => [ 
            '0' => [ 
                'id'                  =>      '1',
                'first'               =>      'test',
                'last'                =>      'test',
            ],
            '2' => [ 
                'id'                  =>      '2',
                'first'               =>      'test',
                'last'                =>      'test',
            ],
            '3' => [ 
                'id'                  =>      '3',
                'first'               =>      'test',
                'last'                =>      'test',
            ],
        ],
    ],
];

Is there a way to changes the standard output that comes with the resource function?

Thank you.

Upvotes: 0

Views: 206

Answers (1)

Matt C
Matt C

Reputation: 548

You can additional data to your response like this.

return (new UsersResource($contacts))
            ->additional([
                'status' => $status,
                'message' => $message,
            ]);

Upvotes: 1

Related Questions