@scottsuhy as far as I understand you want to get the first result, so try to change ->get(); to ->first()->purchase_price_total_LY; in your controller.
Mar 29, 2020
3
Level 1
Beginner: Having trouble understanding how to extract the data out of a result from a query.
I am new to Laravel/Eloquent and I'm having trouble understanding how to extract the data out of a result from a query.
I'm calling the following query in the controller
$purchase['purchase_price_total_LY'] = DB::table('coins')
->select(DB::raw('SUM (purchase_price) as purchase_price_total_LY'))
->where('user_email', '=', auth()->user()->email)
->whereBetween(DB::raw('DATE(purchase_date)'), [$range_lastyr_start, $range_lastyr_end])
->get();
and passing the result back to the view in the return as follows:
return view ('pages.financials', compact('user', 'purchase'));
In the view:
<td>Purchase Price:</td><td><a> {{$purchase['purchase_price_total_LY']}}</a></td>
The result in the view is this:
[{"purchase_price_total_ly":"37"}]
When I do a dd($purchase, $purchase['purchase_price_total_LY']); I see the following:
array:1 [▼
"purchase_price_total_LY" => Illuminate\Support\Collection {#548 ▼
#items: array:1 [▼
0 => {#547 ▼
+"purchase_price_total_ly": "37"
}
]
}
]
Illuminate\Support\Collection {#548 ▼
#items: array:1 [▼
0 => {#547 ▼
+"purchase_price_total_ly": "37"
}
]
}
What's the most appropriate way to extract the 37 out of the array in the view?
Level 53
Please or to participate in this conversation.