Php 此集合实例上不存在映像

Php 此集合实例上不存在映像,php,laravel,laravel-5,Php,Laravel,Laravel 5,我正试图从数据库到视图显示products表中名为image的行中的多个图像,但我得到的错误是,属性[image]在此集合实例上不存在。来自@foreach(json_decode($products->image,true)as$product)循环,我尝试过其他解决方案,但都不起作用 这是密码 控制器 public function store(Request $request) { $Input=$request->all(); $image=array

我正试图从数据库到视图显示products表中名为image的行中的多个图像,但我得到的错误是,
属性[image]在此集合实例上不存在。
来自
@foreach(json_decode($products->image,true)as$product)
循环,我尝试过其他解决方案,但都不起作用

这是密码

控制器

  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(); 

    }
叶片视图

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

欢迎提供任何帮助。

您的产品型号应如下所示,以具有多个图像:

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

public function images() {
    return $this->hasMany('App\images');
}
您可能还需要添加关键点,以防它们不匹配,例如:

return $this->hasMany('App\images', 'product_id', 'id');
完成此操作后,您可以像下面这样迭代图像:

@foreach($products as $product)
   @foreach($product->images as $image)
       <img src="{{url('images',$image->filepath)}}" alt="">
   @endforeach
@endforeach
@foreach($products作为$product)
@foreach($product->图像为$image)
文件路径)}}“alt=”“>
@endforeach
@endforeach

您的产品型号应如下所示,以具有多个图像:

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

public function images() {
    return $this->hasMany('App\images');
}
您可能还需要添加关键点,以防它们不匹配,例如:

return $this->hasMany('App\images', 'product_id', 'id');
完成此操作后,您可以像下面这样迭代图像:

@foreach($products as $product)
   @foreach($product->images as $image)
       <img src="{{url('images',$image->filepath)}}" alt="">
   @endforeach
@endforeach
@foreach($products作为$product)
@foreach($product->图像为$image)
文件路径)}}“alt=”“>
@endforeach
@endforeach

为了让我们不必猜测,请告诉我们错误是在哪一行生成的。始终显示所有错误消息,请不要对其进行总结错误是从“@foreach(json_decode($products->image,true)作为$product)”生成的@riggsfullywouldn最好将此图像信息存储在与产品的
id
链接的另一个表中,与其用json字符串来定义它,不如确认您在
标记中提到了
enctype=“multipart/form data”
。是的,我在@sahilgupta表单中提到了
enctype=“multipart/form data”
,这样我们就不必猜测了,请告诉我们错误是在哪一行生成的。始终显示所有错误消息,请不要对其进行总结错误是从“@foreach(json_decode($products->image,true)作为$product)”生成的@riggsfullywouldn最好将此图像信息存储在与产品的
id
链接的另一个表中,与其用json字符串来定义它,不如确认您在
标记中提到了
enctype=“multipart/form data”
。是的,我在@sahilgupta表单中提到了
enctype=“multipart/form data”
,该关系不在(images table)中存储图像,并且也得到了错误
“未定义属性:stdClass::$images
@aynberCan您可以
var\u dump($products)
并显示其中包含的内容吗?即使我执行
var\u dump
操作,它也会一直显示错误@aynberth var\u dump是查看结构是什么,而不是修复错误。该关系不会在(images table)中存储图像。”我还得到了错误
“Undefined property:stdClass::$images
@aynberCan你可以
var\u dump($products)
并显示其中包含的内容吗?即使我做了
var\u dump
它也会一直显示错误@aynberth var\u dump是查看结构是什么,而不是修复错误。