I have no idea how to explain my question in another way as than by giving an example. Hope this is ok.
I defined two arrays:
$range = ['1000' => '1100', '1100' => '1200', '1200' => '1300'];
and
$years = ['2010', '2011'];
then i try to get data from a given database like:
foreach ($years as $year) {
foreach ($range as $from => $to) {
$result[$year][$from] = Flight::leftJoin('aircrafts', 'flights.LFZ_LFDNR', '=', 'aircrafts.LFZ_LFDNR')
->selectRaw('aircrafts.GEWICHT, SUM(flights.ANZLDG) AS LANDUNGEN')
->whereYear('DATUM', '==', $year)
->where('GEWICHT', '>=', $from)
->where('GEWICHT', '<', $to)
->count();
};
};
which works perfectly with an output like:
{
"2010": {
"1000": 821,
"1100": 979,
"1200": 126,
"1300": 127,
"1400": 69,
"1500": 157,
"1600": 33,
"1700": 364,
"1800": 64,
"1900": 69
},
"2011": {
"1000": 891,
"1100": 1027,
"1200": 112,
"1300": 128,
"1400": 76,
"1500": 135,
"1600": 64,
"1700": 701,
"1800": 96,
"1900": 67
}
}
I changed the $years array to:
$years = ['2010'=>'red','2011'=>'green'];
and the query to:
foreach ($years as $year => $color) {
foreach ($range as $from => $to) {
$result[$year][$from] = Flight::leftJoin('aircrafts', 'flights.LFZ_LFDNR', '=', 'aircrafts.LFZ_LFDNR')
->selectRaw('aircrafts.GEWICHT, SUM(flights.ANZLDG) AS LANDUNGEN')
->whereYear('DATUM', '==', $year)
->where('GEWICHT', '>=', $from)
->where('GEWICHT', '<', $to)
->count();
};
};
what i got:
{
"2010": {
"1000": 0,
"1100": 0,
"1200": 0,
"1300": 0,
"1400": 0,
"1500": 0,
"1600": 0,
"1700": 0,
"1800": 0,
"1900": 0
},
"2011": {
"1000": 0,
"1100": 0,
"1200": 0,
"1300": 0,
"1400": 0,
"1500": 0,
"1600": 0,
"1700": 0,
"1800": 0,
"1900": 0
}
}
and i have no idea why i get just "0"! When i try to debug and check the $year var, it still has the correct value. If I insert e.g. '2010' instead of $year in the ->whereyear(), it works as well. I have no idea what causes the problem. Any help welcome.
many thanks!