0

I want parse data with json in laravel 5.3 but i got message

Undefined property: Illuminate\Support\Collection::$bulan

   public function chart()
    {
      $bulan = [];
      $jumlah = [];
      $data[] = DB::table('data_peminjaman')
                    ->select(DB::raw("DATE_FORMAT(created_at,'%Y') as tahun ,DATE_FORMAT(created_at,'%M') as bulan,COUNT(*) as jumlahdata, DATE_FORMAT(created_at,'%m') as b"))
                    ->groupBy(DB::raw("DATE_FORMAT(created_at,'%M'),DATE_FORMAT(created_at,'%Y'),DATE_FORMAT(created_at,'%m')"))
                    ->orderBy('tahun')
                    ->orderBy('b')
                    ->get();
      foreach ($data as $key ) {
        array_push($bulan, $key->bulan);
        array_push($jumlah, $key->jumlahdata);
      }
      // return view('chart',$data);
      return view('chart', compact('bulan', 'jumlah'));
    }
2
  • What does it mean "I can't push" ? What is "Array Push error"? Please be more specific what the problem is. Commented Aug 19, 2018 at 18:45
  • @MarcinNabiałek i got message 'Undefined property: Illuminate\Support\Collection::$bulan' Commented Aug 19, 2018 at 18:47

1 Answer 1

3

Instead of:

$data[] = DB::table('data_peminjaman')
                    ->select(DB::raw("DATE_FORMAT(created_at,'%Y') as tahun ,DATE_FORMAT(created_at,'%M') as bulan,COUNT(*) as jumlahdata, DATE_FORMAT(created_at,'%m') as b"))
                    ->groupBy(DB::raw("DATE_FORMAT(created_at,'%M'),DATE_FORMAT(created_at,'%Y'),DATE_FORMAT(created_at,'%m')"))
                    ->orderBy('tahun')
                    ->orderBy('b')
                    ->get();

you should use:

$data = DB::table('data_peminjaman')
                    ->select(DB::raw("DATE_FORMAT(created_at,'%Y') as tahun ,DATE_FORMAT(created_at,'%M') as bulan,COUNT(*) as jumlahdata, DATE_FORMAT(created_at,'%m') as b"))
                    ->groupBy(DB::raw("DATE_FORMAT(created_at,'%M'),DATE_FORMAT(created_at,'%Y'),DATE_FORMAT(created_at,'%m')"))
                    ->orderBy('tahun')
                    ->orderBy('b')
                    ->get();

The only difference here is $data[] vs $data.

Also instead of:

foreach ($data as $key ) {
    array_push($bulan, $key->bulan);
    array_push($jumlah, $key->jumlahdata);
}

you can use pluck method:

$bulan = $data->pluck('bulan');
$jumlah = $data->pluck('jumlahdata');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.