scottsuhy
6 months ago
1091
3
Eloquent

Beginner: Having trouble understanding how to extract the data out of a result from a query.

Posted 6 months ago by scottsuhy

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?

Please sign in or create an account to participate in this conversation.