简体   繁体   English

该集合实例上不存在图像

[英]image does not exist on this collection instance

I am trying to display multiple images from the row called image in products table from the database to the view, but am getting this error Property [image] does not exist on this collection instance. 我试图从数据库到视图的产品表中的名为image的行中显示多个图像,但出现此错误Property [image] does not exist on this collection instance. from the @foreach(json_decode($products->image, true) as $product) loop , I have tried other solutions but they don't work. @foreach(json_decode($products->image, true) as $product)循环中,我尝试了其他解决方案,但它们不起作用。

Here are the codes 这是代码

Controller 控制者

  public function store(Request $request) 
  { 

    $Input=$request->all();
    $image=array();
    if($files=$request->file('image')){
        foreach($files as $file){
            $name=$file->getClientOriginalName();
            $file->move('images',$name);
            $image[]=$name;

        }

    } 
   product::create(array_merge($Input,
   [
    'image' => json_encode($image),

    ])); 
    return redirect()->back(); 

    }

Blade view 刀片视图

     @foreach(json_decode($products->image, true) as $product)
      <img src="{{url('images',$product->image)}}" alt="">
     @endforeach

Product model 产品型号

   protected $table='products';
  protected $primaryKey='id';
  protected $fillable=['name','price','image','stock'];

public function images()
{
    return $this->belongsTo('App\product', 'image');
}

Any help will be appriciated. 任何帮助将被申请。

Your product model should look like this, to have multiple images: 您的产品模型应如下所示,以具有多个图像:

protected $table='products';
protected $primaryKey='id';
protected $fillable=['name','price','image','stock'];

public function images() {
    return $this->hasMany('App\images');
}

You may need to add the keys as well, in case they don't match up right, such as : 您可能还需要添加密钥,以防它们不正确匹配,例如:

return $this->hasMany('App\images', 'product_id', 'id');

Once this is done, you can iterate through the images like this: 完成此操作后,您可以像这样遍历图像:

@foreach($products as $product)
   @foreach($product->images as $image)
       <img src="{{url('images',$image->filepath)}}" alt="">
   @endforeach
@endforeach

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM