3

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?

RPichioli
  • 3,245
  • 2
  • 25
  • 29
xTheWolf
  • 1,756
  • 4
  • 18
  • 43
  • 2
    Both relations have a `belongTo`, that's not right. Product should probably be `hasMany`. – Andrei Sep 13 '16 at 12:52
  • @Scarwolf it seems like, in your view, you are getting an array instead of a collection. Could you please check using `$product['image']['thumbnail']` – jaysingkar Sep 13 '16 at 12:56
  • Andrew: Ooh, you're right. Changed it to hasOne, still the same, though. (1 image row for each product). @jaysingkar That's working. Why do laravel returns an array though? How can I change that back to a collection...? – xTheWolf Sep 13 '16 at 12:59
  • sorry @Scarwolf I'm not sure about that. However,you can try passing `$categories` directly without adding it to `$data` array. – jaysingkar Sep 13 '16 at 13:02
  • Using `with` may be overriding your relationships. If you have relationships all the way down, try using just `Categories::all()`, then access everything through the relationships. – aynber Sep 13 '16 at 13:07
  • @jaysingkar have it like this now: `return view('index')->with('categories', $categories);` doesnt, work. Thank you though! – xTheWolf Sep 13 '16 at 13:08
  • @aynber Changed it to `$categories = Category::all();`. Same error: `trying to get property of non-object` – xTheWolf Sep 13 '16 at 13:09
  • How are you defining `$subcategory` in your view? – aynber Sep 13 '16 at 13:10
  • Have you tried dd($data) or print_r($data) ?? – Ketav Sep 13 '16 at 13:12
  • @aynbar By using a `foreach($categories->subcategories as $subcategory)` which works fine. – xTheWolf Sep 13 '16 at 13:16
  • @KetavChotaliya Good catch! As you can see here in line 41: http://pastebin.com/U3FAtcsK image returns an array while prices returns a colletion how it should. But what did I do wrong? – xTheWolf Sep 13 '16 at 13:19

1 Answers1

0

As per your question, You joined two table with relationship. When you fetch data from categories table it will return categories and product data.

As per given link by you: http://pastebin.com/U3FAtcsK, your $data variable contain this type of hierarchy.

category 
  subcategories
    products
      prices
      image

you are trying to display data of image array.

you have to fetch image data like this.

$categories->subcategories->product->image->thumbnail

Understand your Array Hierarchy. You didn't nothing wrong. :)

Ketav
  • 760
  • 1
  • 8
  • 27