更新现有数组中的项目 laravel 5.5

Updating item in exist array laravel 5.5

我有一个产品 table 有一个名称为图像的列,我将所有图像上传到服务器后的名称存储在上面。

但我的问题是,当我想更新任何产品并且想在图片列中添加更多图片时,我遇到了这个错误

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

提前致谢

你需要用到这个方法:

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

我想你用过,$images[]=$fileName; 第 10 行,LHS 是一个数组,RHS 是字符串值。使用 array_push($images, $fileName); 代替。

简而言之,我的意思是:

array_push($images, $fileName); 替换你的 $images[]=$fileName;

这一行:

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

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

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

$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,而不仅仅是逗号分隔列表,并且您现有的数据将不兼容。但是,如果这是新的开发,我建议从这个方法开始。

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

你也可以像我之前提到的那样查看演员表。

替代方式

我不确定你为什么要把这个弄得这么复杂。可能有一个非常明显的原因,但我看不到。

你为什么不创建另一个带有枢轴的模型 ProductImage table product_product_image 这样你就可以简单地在两者上定义属于许多关系

产品

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

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

现在您可以使用我为您的创建方法 post 编辑的相同功能简单地向您的产品添加任何新图像。

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