Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access collection instance as javascript variable?

I was trying to send collection of following Query:

 $monthly_report_chart = DB::table("transactions")
        ->select(DB::raw("Date(updated_at) as today"),DB::raw("SUM(collected_today) as sum"))
        ->groupBy(DB::raw('Date(updated_at)'))
        ->where(DB::raw('Month(updated_at)'),'=',$month)
        ->get();

And i want to access the collection in javascript like this :

{!! json_encode($monthly_report_chart->today) !!}

But it throws following Error :

Property [today] does not exist on this collection instance

How do access collection instance in javascript? Thanks!

like image 338
Hola Avatar asked Feb 12 '18 11:02

Hola


2 Answers

if your collection has today property you can use pluck on collection. E.g

{!! json_encode($monthly_report_chart->pluck('today')) !!}
like image 104
Prashant Prajapati Avatar answered Nov 16 '22 13:11

Prashant Prajapati


Use first():

$monthly_report_chart = DB::table("transactions")
    ->select(DB::raw("Date(updated_at) as today"),DB::raw("SUM(collected_today) as sum"))
    ->groupBy(DB::raw('Date(updated_at)'))
    ->where(DB::raw('Month(updated_at)'),'=',$month)
    ->first();

Or loop through your collections and access individual ones:

@foreach($monthly_report_chart as $report_chart)
     {!! json_encode($report_chart->today) !!}
@endforeach
like image 27
Sohel0415 Avatar answered Nov 16 '22 12:11

Sohel0415