You are returning nested associative arrays, these become JSON objects. If you want a JSON array, then remove the key(s) at the relevant level, e.g.
return response()->json([
'sheets' => [
'test',
'test2'
]
]);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm having issues getting a json reponse from my API as an array.
Here is my code:
Axios request:
axios.get('/api/sheets/all')
.then((response) => {
console.log(response.data.sheets);
})
.catch((error) => {
this.errors = error.response.data;
});
Here is my api method:
public function all()
{
return response()->json([
'sheets' => [
'sheet1' => 'test',
'sheet2' => 'test2'
]
]);
}
When I console log the response, I can see its an object - not an array
{sheet1: "test", sheet2: "test2"}
sheet1: (...)
sheet2: (...)
__ob__: Observer {value: {…}, dep: Dep, vmCount: 0}
get sheet1: ƒ reactiveGetter()
set sheet1: ƒ reactiveSetter(newVal)
get sheet2: ƒ reactiveGetter()
set sheet2: ƒ reactiveSetter(newVal)
__proto__: Object
This is causing issues with my vue component, as it expects an array - not an object.
Any ideas?
You are returning nested associative arrays, these become JSON objects. If you want a JSON array, then remove the key(s) at the relevant level, e.g.
return response()->json([
'sheets' => [
'test',
'test2'
]
]);
Please or to participate in this conversation.