3

I am currently working on a Laravel API and wish to return some data as a JSON array. I am able successfully do it with most data, however one query returns an associative array instead of an array of the objects. enter image description here

Here, you can see if I return a list of all the records, or where the type is food, a normal array of objects is returned. This will then look like [...] when returned as JSON data.

The problem is, when I wish to return type of drink, Laravel wishes to return an associative array of objects which I do not wish for. This would make my json look like {...}

Why is this happening and what could I do to stop this?

1 Answer 1

7

Check this documentation

https://laravel.com/docs/5.8/collections#method-values

Or try doing this to this to your response

return response()->json([
    'prop' => $prop->values()->toArray()
]);

Sign up to request clarification or add additional context in comments.

1 Comment

All that needs changing is adding ->values() after the where() function. Thanks for answering!

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.