@tinybot if you have used eloquent model and relation instead of query builder, you would have got the result like u expected, or else foreach your result and create an new array and structure that new array like u expected and return he new array
Apr 20, 2018
3
Level 2
How to manipulate query result into JSON-ready layout?
This is probably more of a PHP question than it is a Laravel-specific question - but here goes!
I'm trying to prepare the result of an Eloquent Query for use in a charting tool (in my case, Highcharts):
$rows = DB::table('pushups')
->join('users', 'pushups.user_id', '=', 'users.id')
->selectRaw('users.name as name, sum(pushups.amount) as amount, pushups.date as date')
->whereYear('datetime', '=', date('Y'))
->whereMonth('datetime', '=', date('m'))
->groupBy('name', 'date')
->get();
The result looks fine:
[{"name":"ace","amount":"100","date":"2018-04-16"},{"name":"ace","amount":"100","date":"2018-04-17"},{"name":"ace","amount":"100","date":"2018-04-18"},{"name":"bern","amount":"50","date":"2018-04-17"},{"name":"bern","amount":"100","date":"2018-04-18"}]
But now I'm trying to get it to look like this:
[{
name: "ace",
data: [
[Date.UTC(2018, 03, 16), 100],
[Date.UTC(2018, 03, 17), 200],
[Date.UTC(2018, 03, 18), 300],
[Date.UTC(2018, 03, 19), 400],
]
}, {
name: "bern",
data: [
[Date.UTC(2018, 03, 16), 100],
[Date.UTC(2018, 03, 17), 150],
[Date.UTC(2018, 03, 18), 170],
[Date.UTC(2018, 03, 19), 190],
]
}, {
name: "chuck",
data: [
[Date.UTC(2018, 03, 16), 100],
[Date.UTC(2018, 03, 17), 200],
[Date.UTC(2018, 03, 18), 250],
[Date.UTC(2018, 03, 19), 300],
]}
]
I believe this is going to require nested loops? Just can't wrap my head around it at the moment. Any tips or help is greatly appreciated!
Please or to participate in this conversation.