【问题标题】:Updating item in exist array laravel 5.5更新现有数组laravel 5.5中的项目
【发布时间】:2018-06-17 16:18:41
【问题描述】:

我有一个 products 表有一个列名 images 我在上传到服务器后存储所有图像的名称。

但我的问题是当我想更新任何产品并且我想在图片列中添加更多图片时出现此错误

Array to string conversion (SQL: update `products` set `pro_name` = Test, `images` = A6FC3-091547-2Nx.jpg, `updated_at` = 2018-06-17 03:29:20 where `id` = 1)

这是我的代码:

 public function update(Request $request, $id){

                $images=array();
                if($files=$request->file('images')){
                    foreach($files as $file){
                        $extension = $file->getClientOriginalExtension();
                        $fileName = str_random(5)."-".date('his')."-".str_random(3).".".$extension;
                        $upfolder = "admin/UploadImages"."/";
                        $file->move($upfolder , $fileName);
                        $images[]=$fileName;
                    }
                }


                $pros = Product::find($id);
                $pros->pro_name =   $request->input('pro_name');    
                $pros->pro_code =   $request->input('pro_code');    
                $pros->aval     =   $request->input('aval');    
                $pros->price    =   $request->input('price');   
                $pros->colors   =   $request->input('colors');  
                $pros->images   =   array_merge(explode(",",$pros->images), $images);
                $pros->cat_id   =   $request->input('cat_id');  
                $pros->hide     =   $request->input('hide');    
                $pros->save();
return redirect('products');
    }

提前致谢

【问题讨论】:

    标签: laravel laravel-5.5


    【解决方案1】:

    正如其他人所说,您正在尝试将数组存储为字符串。 使用 php 的 implode() 函数是您需要寻找的地方。

    您也可以查看我之前提到的演员表。

    另一种方式

    我不确定你为什么要为自己做这么复杂的事情。可能有一个非常明显的原因,但是我看不到。

    您为什么不直接使用数据透视表 product_product_image 创建另一个模型 ProductImage,这样您就可以简单地在两者上定义属于多个关系

    产品

    public function images()
    {
        return $this->belongsToMany(ProductImage::class);
    }
    

    产品图片

    public function products()
    {
        return $this->belongsToMany(Product::class);
    }
    

    这样当你创建一个产品时

    你可以这样做:

    $product = Product::create([
        //create your product here.
    ]);
    
    foreach($request->images[] as $image){
        $product->images()->attach();    
    }
    

    然后,当您更新图像时,您可以将它们全部显示在您的页面中,也许是一个简单地在单击时删除图像的 ajax 调用。简单地调用的东西:

    $product = Product::find($request->product_id);
    $image = ProductImage::find($request->image_id);
    $product->images()->detach($image);
    

    这意味着在您将整个产品更新发布回服务器之前,您想要删除的任何内容都已完成。

    现在,您可以使用我为您的 create 方法发布的相同功能,简单地将任何新图像添加到您的产品中。

    再一次,你这样做可能是有原因的,但我只是向你展示了一种利用 laravel 功能的替代方法。

    【讨论】:

      【解决方案2】:

      你需要使用这个方法:

      array_push($arrayName, Item1, [Item2, Item3]);
      

      我想你已经使用了,$images[]=$fileName; 在第 10 行,LHS 是一个数组,RHS 是字符串值。请改用array_push($images, $fileName);

      简而言之,我的意思是:

      将您的$images[]=$fileName; 替换为array_push($images, $fileName);

      【讨论】:

        【解决方案3】:

        在这一行:

        $pros->images = array_merge(explode(",", $pros->images), $images);
        

        array_merge() 返回一个数组。当您保存记录时,它会尝试将该数组转换为字符串,从而为您提供您所看到的错误。

        根据您当前的逻辑,您需要在合并后内爆数组:

        $pros->images = implode(",", array_merge(explode(",",$pros->images), $images));
        

        现在字符串将成功保存到数据库中。


        另一个特定于 Laravel 的选项是将 images 字段添加到 Product 模型上的 $casts 数组中。

        protected $casts = [
            'images' => 'array',
        ];
        

        现在,数据将作为字符串存储在数据库中,但 Laravel 会在您访问时自动将其转换为数组。

        如果您如上所述更新了 $casts 属性,您的代码将是:

        $pros->images = array_merge($pros->images, $images);
        

        如果您在数据库中有现有数据,您将无法简单地进行此更改,因为 Laravel 将数组存储为 json,而不仅仅是逗号分隔的列表,并且您现有的数据将不兼容。但是,如果这是新的发展,我建议从这种方法开始。

        【讨论】:

          猜你喜欢
          • 2018-04-16
          • 1970-01-01
          • 2018-02-11
          • 2018-03-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-07
          相关资源
          最近更新 更多