Skip to content
Advertisement

Array of images in laravel

While I am retrieving array of images I get this error:

Trying to get property ‘images’ of non-object (View: C:xampphtdocsuserresourcesviewsproductview.blade.php)

public function show($id)
    {
        //

         $model = product::find($id);
        return view("product.view", [
            'data' => $model
        ]);
    }

view blade:

@foreach($data as $image)
       <tr>
           <td> <?php foreach (json_decode($image->images)as $picture) { ?>
                 <img src="{{ ('/public/images/'.$picture) }}" style="height:120px; width:200px"/>
                <?php } ?>
           </td>
      </tr>
        @endforeach

Advertisement

Answer

you need to clean and fix your code:

your controller function:

public function show($id)
{
         //find one product from products table  
         $product = product::find($id);
        //pass product to blade file by compact funcation
        return view("product.view", compact('product');
}

in blade file:

       <tr>
           <td>
          {{-- get product images from images json column   --}}
          @foreach(json_decode($product->images) as $picture)
                 <img src="{{ ('/public/images/'.$picture) }}" style="height:120px; width:200px"/>
          @endforeach
           </td>
      </tr>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement