skip to Main Content

I Want to Send My Query Result to ApexCharts, but the results is not what i am intended

this is my query

 $queryPengadaanNonTender = DB::table('siapel_dokumen')
            ->where([['id_jenis_dokumen', '=','1'],['progres','=','100']])
            ->join('siapel_pengadaan', 'siapel_dokumen.id_jenis_pengadaan', '=', 'siapel_pengadaan.id')
            ->select(
                    DB::raw('COUNT(DISTINCT nama_paket) AS jml'),
                    DB::raw('jenis_pengadaan as pengadaan'),
                    DB::raw('tahun as tahun')
                    )
                    ->groupBy('pengadaan','tahun')
                    ->orderBy('tahun')
                    ->get();

            $pengadaanNonTender = $queryPengadaanNonTender->groupBy('pengadaan','tahun')->map(function ($groupedItems) {
                return $groupedItems->map(function ($item)  {
                    return [
                        'tahun' => $item->tahun,
                        'jml' => $item->jml,
                    ];
                });
            })->toArray();

And This Is The Output

"pengadaanNonTender" => array:7 [▶
    "Pengadaan Langsung - Barang" => array:2 [▶
      0 => array:2 [▶
        "tahun" => 2022
        "jml" => 9
      ]
      4 => array:2 [▶
        "tahun" => 2023
        "jml" => 20
      ]
    ]
    "Pengadaan Langsung - Konstruksi" => array:2 [▶
      1 => array:2 [▶
        "tahun" => 2022
        "jml" => 67
      ]
      6 => array:2 [▶
        "tahun" => 2023
        "jml" => 192
      ]
    ]
    "Tender-Konstruksi" => array:1 [▶
      2 => array:2 [▶
        "tahun" => 2022
        "jml" => 1
      ]
    ]
    "e-Katalog" => array:1 [▶
      3 => array:2 [▶
        "tahun" => 2023
        "jml" => 2
      ]
    ]
    "Pengadaan Langsung - Jasa Lainnya" => array:1 [▶
      5 => array:2 [▶
        "tahun" => 2023
        "jml" => 16
      ]
    ]
    "Pengadaan Langsung-Konsultan" => array:1 [▶
      7 => array:2 [▶
        "tahun" => 2023
        "jml" => 10
      ]
    ]
    "Pengadaan Langsung-Konsultansi" => array:1 [▶
      8 => array:2 [▶
        "tahun" => 2023
        "jml" => 9
      ]
    ]
  ]
  "pengadaanTender" => array:3 [▶
    "Tender-Konstruksi" => array:2 [▶
      0 => array:2 [▶
        "tahun" => 2022
        "jml" => 11
      ]
      3 => array:2 [▶
        "tahun" => 2023
        "jml" => 20
      ]
    ]
    "Pengadaan Langsung - Barang" => array:1 [▶
      1 => array:2 [▶
        "tahun" => 2023
        "jml" => 1
      ]
    ]
    "Tender Barang" => array:1 [▶
      2 => array:2 [▶
        "tahun" => 2023
        "jml" => 1
      ]
    ]
  ]

when i send the output to chart

this is what i get

enter image description here

i want to get unique year for my categories chart. i’ve tried everything but i still can’t solve this.

PS : Sorry for my broken english

2

Answers


  1. The following will give you a collection of unique years from your $pengadaanNonTender array. You can then convert this collection to an array and use it as chart categories.

    $uniqueYears = collect($pengadaanNonTender)->flatMap(function ($items) {
        return array_column($items, 'tahun');
    })->unique()->values();
    
    
    Login or Signup to reply.
  2. In your current code, you are grouping the data by ‘pengadaan’ and ‘tahun’. To achieve what you want, you should group the data only by ‘tahun’ and then organize the counts for each ‘pengadaan’ within that year. Here’s how you can modify your code:

    $queryPengadaanNonTender = DB::table('siapel_dokumen')
            ->where([['id_jenis_dokumen', '=', '1'], ['progres', '=', '100']])
            ->join('siapel_pengadaan', 'siapel_dokumen.id_jenis_pengadaan', '=', 'siapel_pengadaan.id')
            ->select(
                DB::raw('COUNT(DISTINCT nama_paket) AS jml'),
                DB::raw('jenis_pengadaan as pengadaan'),
                DB::raw('tahun as tahun')
            )
            ->groupBy('tahun', 'pengadaan') // Group by 'tahun' first
            ->orderBy('tahun')
            ->get();
    
        $pengadaanNonTender = $queryPengadaanNonTender->groupBy('tahun')->map(function ($groupedItems) {
            return $groupedItems->map(function ($item) {
                return [
                    'tahun' => $item->tahun,
                    'jml' => $item->jml,
                    'pengadaan' => $item->pengadaan,
                ];
            });
        })->toArray();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search