forse ce l'ho fatta:
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 = array();


      foreach ($queryMediatori as $qm) {
        $itemQm = $qm;
        $itemQm['timesheets'] = Timesheet::query()
            ->select([
                'id_timesheet',
                'id_timesheet_old'
            ])
            ->where('cod_anagrafica', $qm->id_anagrafica)
            ->whereIn('stato_ore', explode(',', $statoOre))
            ->get();


        array_push($arrMediatori, $itemQm);
      }


      $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);
  }