Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

JSON response with message

Hello,

I work on an existing project (Laravel / Vue) in API mode. For now the controller methods return only a single value.

$data = 'Some data';
return response()->json($data);

If I modify the returned value in an array, it won't work any more with the frontend.

$data = 'Some data';
return response()->json(['data' => $data]);

Is there a possibility to return both at the same time a simple value and in addition a key / value pair ?

Perhaps a single value and via another way a message ?

$data = 'Some data';
return response()->json([$data])->withMessage('message'); // it won't work, but is there a possibility to do something like this ? I don't have tried it but not seen in the documentation

I think that I already have the answer, but perhaps there is a solution I don't know.

Thanks for your help.

Vincent

0 likes
4 replies
webrobert's avatar
Level 51

why can't you change the response in vue to use an array?

1 like
vincent15000's avatar

@webrobert I can do that ... but first I wanted to explore any other way without having to change anything in VueJS. But that's what I thought, it's not possible to do without an array. So it will be necessary to change how the response is handled in VueJS. I will tell to the front dev that he has to change something in the front. ;)

Please or to participate in this conversation.