Reputation: 7656
I got an query and it return object. I want to make each of the object become an array:
Here is my query:
$transactions['transactions'] = Transaction::groupBy(DB::raw('DATE(created_at)'))
->selectRaw('FLOOR(SUM(amount)) as total, DATE(created_at) as date')
->orderBy('created_at')
->get()->toArray;
Here is the result I get:
"transactions": [
{
"total": "88781",
"date": "2015-01-01"
},
{
"total": "95630",
"date": "2015-01-02"
},
{
"total": "57857",
"date": "2015-01-03"
},
{
"total": "85252",
"date": "2015-01-04"
},
{
"total": "64763",
"date": "2015-01-05"
}
]
Expected result:
"transactions": [
[
"88781",
"2015-01-01"
],
[
"95630",
"2015-01-02"
],
[
"57857",
"2015-01-03"
],
[
"85252",
"2015-01-04"
],
[
"64763",
"2015-01-05"
]
]
I've tried to json_decode
but got no luck.
Any solution?
Upvotes: 2
Views: 86
Reputation: 1829
You could try mapping over the values in the collection and converting them to non-associative arrays:
$transactions_result = Transaction::groupBy(DB::raw('DATE(created_at)'))
->selectRaw('FLOOR(SUM(amount)) as total, DATE(created_at) as date')
->orderBy('created_at')
->get();
$transactions_result_as_array = $transactions_result->map(function($item, $key){
return [$item->total, $item->date];
});
$transactions['transactions'] = $transactions_result_as_array->toArray();
Upvotes: 2