[Vuejs]-Array is returned as an object using resource

1👍

There is a bit of confusion here, mostly caused by PHP lingo.

In PHP lingo an associative array is still an array. But an associative array is actually a dictionary.

Other programming languages don’t see an associative array (dictionary) as an array and as such have a different vocabulary.

Your data structure is actually a dictionary, and not a numerical indexed array.

From a JSON perspective if your data structure has non-numerical keys then it gets translated to an object.

Your confusion stems from the fact that is_array will return true if the variable is a zero based indexed array, when in fact it returns true for associate arrays also.

0👍

It’s in the definition. Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON. Check the Resource docs

If you are expecting an array in return then I suggest skip resource and pass the data directly from the controller using ->toArray(). But then again you are using axios in your vuejs then I strongly recommend to stick with json format as an expected response.

Leave a comment