Is there an easy (easier) way to split up my JSON? I'm not so good with JSON but here is my situation;
I have a table of payments and a table of locations. There are 10 payments at location A and 5 payments at location B.
my payments table is like;
CREATE TABLE `payments` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`amount` decimal(15,2) NOT NULL,
`location_id` int(7) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=63678 DEFAULT CHARSET=latin1;
my locations table is like;
CREATE TABLE `locations` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL DEFAULT '',
`auth_key` varchar(32) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
So I'd like my JSON to be something like;
location:[
{"A":
"auth_key": "--justanexample--",
"payments":[
{"id":"1", "amount":"15.15"},
{"id":"3", "amount":"11.85"},
{"id":"4", "amount":"18.95"}
]},
{"B":
"auth_key": "--justanotherexample--",
"payments":[
{"id":"2", "amount":"15.00"},
{"id":"5", "amount":"12.77"},
{"id":"6", "amount":"13.45"}
]}
]
At the moment, I am using Eloquent and I'm creating two nested loops, first to get all the locations and then to get all the payments per location;
$locations = $app->location->get();
foreach ($locations as $location) {
$payments = $app->payment->get();
foreach ($payments as $payment) {
echo json_encode($payment);
Or should I use an array function here instead and then encode that?