In my Controller I have:
public function showMainPage()
{
$categories = Category::with('subcategories.products.prices', 'subcategories.products.image')->get();
$data = array(
"categories" => $categories,
);
return view('index')->with($data);
}
When I reference this in my view like this:
@foreach($subcategory->products as $product)
<img src="{{ $product->image->thumbnail }}" alt="">
I get a Trying to get property of non-object
error.
This is my relationship:
Product.php
public function image()
{
return $this->belongsTo('App\ProductImage');
}
This is my ProductImage Relationship:
public function product()
{
return $this->belongsTo('App\Product');
}
What is wrong there?