0

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.

1 Answer 1

1

You can additional data to your response like this.

return (new UsersResource($contacts))
            ->additional([
                'status' => $status,
                'message' => $message,
            ]);
Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm, Thanks, Matt!

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.