5

I'm executing a query like this as well as some others and returning them via response()->json().

$transactions = Transaction::where('created_at', '>=',
    now()->firstOfYear())->get()->groupBy(function ($transaction)
{
    return Carbon::parse($transaction->created_at)->format('M');
});

return response()->json([
    'user' => $user->toArray(),
    'transactions' => $transactions->toArray()
]);

However, while transactions is an Array in php, when it goes through response()->json it gets turned into an Object. I was hoping someone could tell me how I can prevent this and keep it as an array so I can iterate over it?

Thanks.

Picture of transactions output as requested. (Had to blur a lot of stuff due to sensitive info.)

5
  • Is it an associative array? I assume it is. Javascript does not have a concept of associative arrays and will map to an object. If you can show your data, I can help you with mapping it to something that will work as an array. Commented Dec 4, 2019 at 13:05
  • the above would return an array of objects, which to me seems legit Commented Dec 4, 2019 at 13:35
  • @JeremyHarris I don't really understand what you mean. I don't want to additionally map anything. I'm trying to figure out WHY it is converting an array into an object and prevent it from doing that. It only happens when using "groupBy". Commented Dec 4, 2019 at 13:39
  • Do a var_dump of $user->toArray() and show us the output. Is it an associative array? If so, javascript does not have associative arrays and json_encode will output it as an object. Commented Dec 4, 2019 at 13:40
  • @JeremyHarris I've added a picture in OP of what the structure looks like AFTER the groupBy. Commented Dec 4, 2019 at 13:49

2 Answers 2

7

Your array is keyed with month names, meaning it is an associative array. If you want the JSON to be an array, you will need your PHP array to be indexed numerically.

One option you can do is this (untested):

$userArray = [];
foreach ($user as $key => $value) {
    $userArray[] = (object) [
        'month' => $key,
        'data' => $value,
    ];
}
return response()->json([
    'user' => $userArray,
    'transactions' => $transactions->toArray()
]);

That will make it a numerically indexed array of objects with the month being a property on the object and another property containing the rest of the data.

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

Comments

0

A solution I used before is to check on the frontend whether the data is array or an object, and if it is an object just convert it to array. Associative arrays will get converted to objects in javascript unless its keys start from 0 and increment like a normal arrays index.

An example of doing this:


window.axios.post('api/endpoint', data)
    .then(res => {
        const transactions = Array.isArray(res.data.transactions)
            ? res.data.transactions
            : Object.keys.(res.data.transactions).map(key => response.data.transactions[key]);
    })



Comments

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.