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!
if your collection has today property you can use pluck on collection. E.g
{!! json_encode($monthly_report_chart->pluck('today')) !!}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With