ciao!
sto usando laravel, ma non credo sia determinante.
in sostanza devo ottenere un json di questo tipo:
codice:
{
"data": [
{
"questura": "Bologna",
"mese": "dicembre",
"anno": "2024",
"dirigente": "",
"stato": "ESEGUITE,PRENOTATE",
"totale_minuti": "1800",
"firma_timestamp": null,
"timesheets_mediatori": [
{
"nome": "DIRIGENTE 4391",
"cognome": "DE SANTIS",
"id_anagrafica": 4391,
"totale_minuti": "1800",
"timesheets": [
{
"tlo_id": "gv1201",
"tlo_timesheet": "gv1201"
}
]
}
]
}
]
}
al momento ho fatto questo codice:
codice:
public function getDirigenteFunzionario($anag, $statoOre, $anno, $mese): JsonResponse {
$data['data'] = array();
$format = new IntlDateFormatter('it_IT.UTF-8',
IntlDateFormatter::NONE,
IntlDateFormatter::NONE,
NULL,
NULL,
"MMMM");
$monthName = datefmt_format($format, mktime(0, 0, 0, $mese));
$query = TimesheetOra::query()
->select([
'citta_progetto',
'cod_anagrafica',
DB::raw('SUM(tlo_minuti_lavorati) AS totale_minuti')
])
->join('timesheets', 'id_timesheet', '=', 'tlo_timesheet')
->join('ps_progetto_sede', 'id_sede', '=', 'cod_sede_progetto')
->join('progetto_sedi', 'id_progetto_sede', '=', 'id_sede')
->where('tlo_fascia_oraria', 0)
->whereRaw("MONTH(tlo_data_da) = $mese")
->whereRaw("YEAR(tlo_data_da) = $anno")
->where('cancellato', 0)
->where('cod_anagrafica', $anag)
->where('id_anagrafica', $anag)
->whereIn('stato_ore', explode(',', $statoOre))
->groupBy('id_sede')
->get();
foreach ($query as $var) {
$codAnagrafica = $var->cod_anagrafica;
$queryMediatori = TimesheetOra::query()
->select([
'nome',
'cognome',
'id_anagrafica',
DB::raw('SUM(tlo_minuti_lavorati) AS totale_minuti')
])
->join('timesheets', 'id_timesheet', '=', 'tlo_timesheet')
->join('users', 'cod_anagrafica', '=', 'id_anagrafica')
->where('tlo_fascia_oraria', 0)
->whereRaw("MONTH(tlo_data_da) = $mese")
->whereRaw("YEAR(tlo_data_da) = $anno")
->where('cancellato', 0)
->where('cod_anagrafica', $codAnagrafica)
->whereIn('stato_ore', explode(',', $statoOre))
->groupBy('id_anagrafica')
->get();
$arrMediatori = $queryMediatori->toArray();
foreach ($queryMediatori as $qm) {
$arrMediatori['timesheets'] = array();
$queryTs = Timesheet::query()
->select([
'id_timesheet',
'id_timesheet_old'
])
->where('cod_anagrafica', $qm->id_anagrafica)
->whereIn('stato_ore', explode(',', $statoOre))
->get();
array_push($arrMediatori['timesheets'], $queryTs);
}
$item = array(
"questura" => $var->citta_progetto,
'mese' => $monthName,
'anno' => $anno,
'dirigente' => '',
'stato' => $statoOre,
'totale_minuti' => $var->totale_minuti,
'firma_timestamp' => null,
'timesheets_mediatori' => $arrMediatori,
);
array_push($data['data'], $item);
}
return response()->json($data);
}
che però mi da questo:
codice:
{
"data": [
{
"questura": "Bologna",
"mese": "dicembre",
"anno": "2024",
"dirigente": "",
"stato": "ESEGUITE,PRENOTATE",
"totale_minuti": "1800",
"firma_timestamp": null,
"timesheets_mediatori": {
"0": {
"nome": "DIRIGENTE 4391",
"cognome": "DE SANTIS",
"id_anagrafica": 4391,
"totale_minuti": "1800"
},
"timesheets": [
[
{
"id_timesheet": 48,
"id_timesheet_old": 135958
},
{
"id_timesheet": 49,
"id_timesheet_old": 135959
}
]
]
}
}
]
}
in sostanza non riesco a creare il nome di array timesheets.
dove sto sbagliando??