The keys aren't lost. Keys will still start at 0 for the json output
As fourth parameter json method accepts json_encode options. You can use JSON_FORCE_OBJECT to encode array as object.
echo response()->json(['one', 'two'], 200, [], , JSON_FORCE_OBJECT );
// {"0":"one","1":"two"}
organizm238, semalley liked this reply
Why would you force the JSON method to encode an array as an object? Unless you want to pass an associative array to javascript, keep your variable types consistent.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community