首页 > 解决方案 > laravel 中的图片数组

问题描述

在检索图像数组时,出现此错误:

试图获取非对象的属性“图像”(查看:C:\xampp\htdocs\user\resources\views\product\view.blade.php)

public function show($id)
    {
        //

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

查看刀片:

@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

标签: phplaravel

解决方案


你需要清理和修复你的代码:

你的控制器功能:

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');
}

在刀片文件中:

       <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>

推荐阅读