title: Unable to display image from database - Laravel
body: <p>I have a relationship with product and images. The user can upload many images for a product so I have a product table and product_images table. </p>
  
  <p>When I try to get to fetch the image from the database, I get No image available in my view. But I am able to save to the path attribute in my database. </p>
  
  <p>Why could this be happening?</p>
  
  <p><strong>product_images</strong></p>
  
  <pre><code>id
  
  product_id
  
  path
  </code></pre>
  
  <p><strong>Product</strong></p>
  
  <pre><code>public function images()
  {
     return $this-&gt;hasMany(Image::class);
  }
  </code></pre>
  
  <p><strong>Image</strong></p>
  
  <pre><code> public function products()
   {
      return $this-&gt;belongsTo(Product::class);
   }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>     public function index()
       {   
           $products = Product::where('type', 'BASIC')-&gt;inRandomOrder()-&gt;take(6)-&gt;get();
           return view('home.index',compact('products');   
       }
  
   $product_image = Product::create($request-&gt;all());
  
          if ($request-&gt;hasFile('image'))
           {
              $request-&gt;file('image')-&gt;store('uploads/catalog/images');
  
              // ensure every image has a different name
              $file_name = $request-&gt;file('image')-&gt;hashName();
  
              // save new image $file_name to database
              $product_image-&gt;images()-&gt;update(['image' =&gt; $file_name]);
          }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>@foreach($products as $product)
      @foreach($product-&gt;image as $img)
        &lt;div class="product-item men"&gt;
            &lt;div class="product discount product_filter"&gt;
                &lt;div class="product_image"&gt;
                  &lt;a  href="{{ route('product.view', $product-&gt;slug)}}"&gt;&lt;img src="{{$img}}" alt=""&gt;&lt;/a&gt;
                &lt;/div&gt;
                &lt;!-- &lt;div class="favorite favorite_left"&gt;&lt;/div&gt; --&gt;
                &lt;!-- &lt;div class="product_bubble product_bubble_right product_bubble_red d-flex flex-column align-items-center"&gt;&lt;span&gt;offer&lt;/span&gt;&lt;/div&gt; --&gt;
                &lt;div class="product_info"&gt;
                  &lt;h6 class="product_name"&gt;&lt;a href="{{ route('product.view', $product-&gt;slug)}}"&gt;{{$product-&gt;name}}&lt;/a&gt;&lt;/h6&gt;
                &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      @endforeach
  @endforeach
  </code></pre>
  
  <p><strong>dd($img)</strong></p>
  
  <pre><code>ProductImage {#523 ▼
    #fillable: array:3 [▼
      0 =&gt; "product_id"
      1 =&gt; "path"
      2 =&gt; "is_main_image"
    ]
    #connection: "mysql"
    #table: null
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: false
    #attributes: array:6 [▼
      "id" =&gt; 1
      "product_id" =&gt; 1
      "path" =&gt; "/Users/emmnock/laravel-ecommerce/public/uploads/catalog/ZsT4VwjWS5tLvIl0AKV6sAOxHFrwzwII5FMVUVkP.png"
      "is_main_image" =&gt; null
      "created_at" =&gt; "2018-03-07 10:04:34"
      "updated_at" =&gt; "2018-03-07 10:04:34"
    ]
    #original: array:6 [▶]
    #changes: []
    #casts: []
    #dates: []
    #dateFormat: null
    #appends: []
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #hidden: []
    #visible: []
    #guarded: array:1 [▶]
  }
  </code></pre>