Use instance of data collection as javascript variable?

I tried to send an assembly of the following request:

$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 as follows:

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

But this causes the following error:

The [today] property does not exist in the collection instance.

How is an access collection instance in javascript? Thanks!

+5
source share
2 answers

if your collection has a property that you can use to collect. For instance,

 {!! json_encode($monthly_report_chart->pluck('today')) !!} 
+6
source

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 :

 @foreach($monthly_report_chart as $report_chart) {!! json_encode($report_chart->today) !!} @endforeach 
+2
source

Source: https://habr.com/ru/post/1275311/


All Articles