Ray
Ray

Reputation: 3060

laravel query - call to underfined property - collection

I'm trying to attach some data to a view and have been able to do this a number of times with success. However my latest page is throwing an error:

Undefined property: Illuminate\Database\Eloquent\Collection::$pay_method

In my controller I have the following query:

$receipt = Receipt::where('invoice_id', '=', $id)->get();

$id is a variable holding the invoice id.

I'm passing the object to the view as follows:

return View::make('greenfee.edit')
        ->with('receipt', $receipt);

In the view - to test I am simply doing:

{{ $receipt->pay_method }}

'pay_method' being a column in the table.

This throws the error.

To test further I've tested $receipt contains data using print_r and the property is definitely there - here's an extract:

[pay_method] => card

and is the value I would expect.

So - what am I missing and misunderstanding here?

Also - when should I (or not) use get() on a query?

All help appreciated Ta

Upvotes: 3

Views: 2326

Answers (1)

Ryun
Ryun

Reputation: 729

A Collection is basically an array of results, so you would have to iterate over it with a for loop or something. However, If you are trying to just fetch one record/row you should use:

$receipt = Receipt::where('invoice_id', '=', $id)->first();

Upvotes: 3

Related Questions