Search code examples
phplaravellaravel-5

Updating item in exist array laravel 5.5


I Have a products table has a column name images i store on it the names of all images after i upload them to server.

But my problem is when i want to update any products and i want add more pictures to the images column i got this error

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)

and this is my code:

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

thanks in advance


Solution

  • Like others have said you are trying to store an array as a string. Using php's implode() function is where you need to be looking.

    You can also looking into casts as mentioned before me.

    alternative way

    I am not sure why you are making this so complex for yourself. There may be a really obvious reason, however I can't see it.

    Why don't you just create another model ProductImage with a pivot table product_product_image that way you can simply define a belongs to many relationship on both

    Product

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

    ProductImage

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

    This way when you create a product

    You can do something like:

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

    Then when you are updating your images you can display them all within your page, maybe an ajax call that simple deletes an image when clicked on. Something that simply calls:

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

    This means that any you want to remove are already done before you post your whole product update back to the server.

    Now you can simply add any new images to your product using the same functionality I posted for your create method.

    Again, there may be a reason why you're doing it the way you are but I'm just showing you an alternative way that makes use of laravels functionality.