I have a basic application which stores expenses in a table containing the following columns:
iduser_idquote_id(nullable)titletypenet
The quote_id column is linked to my quotes table; however, there can also be entries in the table without a quote_id.
I'm trying to output all expenses by using the relationship expenses() on my user model.
@foreach(Auth::user()->expenses as $e)
{{ $e->id }}
{{ $e->net }}
{{ $e->quote->title }}
{{ $e->quote_id }}
@endforeach
On my expenses model, I also have a relationship between the expense and the quote.
public function quote()
{
return $this->hasMany('App\Quote', 'id','quote_id');
}
This is great for when there is always a quote_id in the table however some entries don't have a quote_id and I get the following error:
Undefined property: Illuminate\Database\Eloquent\Collection::$title
How can I resolve this?
dd($e->quote)and see what it returns; betcha it isn't a singlequoteobject. In fact I can say for sure that it is not, since the relationship ishasMany{{ $e->quote ? $e->quote->title : "N/A" }}or something like that.