Farshad
Farshad

Reputation: 2000

save 2 arrays simultaneously in laravel

I want to save product images array and color_id arrays in them same order so here now i have 2 arrays : images and color_id and i know that the first item in images array is equal to first item in color_id array now for that i am doing like below :

public function uploadImages($data, $product,$color_id = null,$size_id = null)
{
        $previousImageIds = $product->images()->pluck('id');
        $i = null;
        if (isset($data['images'])) {
            foreach ($data['images'] as $imageId => $image) {
              $file = 'images.' . $imageId;
                $dir = 'product/' . $product->id;
                if (str_contains($imageId, 'image_')) {
                    if (request()->hasFile($file)) {
                        $i++;
                        $this->create([
                                'path' => request()->file($file)->store($dir),
                                'product_id' => $product->id,
                                'product_color_id' => $color_id[$i],
                                'product_size_id'=> $size_id ,
                            ]);
                    }
                } 
        }
}

Now when i pass these 2 arrays to this function as below :

^ array:3 [▼
  "image_1" => Illuminate\Http\UploadedFile {#756 ▶}
  "image_2" => Illuminate\Http\UploadedFile {#790 ▶}
  "image_3" => Illuminate\Http\UploadedFile {#753 ▶}
]

and color array as below :

^ array:3 [▼
  0 => "3"
  1 => "1"
  2 => "2"
]

I get this error :

Undefined offset: 3 //on this line 'product_color_id' => $color_id[$i],

Now it some times works some times not is there any possible way to sync the indexes of 2 arrays and dont let them throw error . thanks

Upvotes: 0

Views: 86

Answers (2)

Sehdev
Sehdev

Reputation: 5682

You are getting this error because you are incrementing $i++ before using it. So $i becomes 1 in first iteration then 2 and then 3. But in your colors array index 3 does not exists

To resolve this you should add $i++ at the end. Also check isset and !empty of your color_id array index for error handling in case index does not exists

  public function uploadImages($data, $product,$color_id = null,$size_id = null) {
    $previousImageIds = $product->images()->pluck('id');
    $i = null;
    if (isset($data['images'])) {
        foreach ($data['images'] as $imageId => $image) {
          $file = 'images.' . $imageId;
            $dir = 'product/' . $product->id;
            if (str_contains($imageId, 'image_')) {
                if (request()->hasFile($file)) {
                   $colorId = "";
                   if (isset($color_id[$i]) && !empty($color_id[$i])) {
                           $colorId = $color_id[$i];
                   }

                   $this->create([
                      'path' => request()->file($file)->store($dir),
                      'product_id' => $product->id,
                      'product_color_id' => $colorId,
                      'product_size_id'=> $size_id ,
                   ]);
                }
            } 
            $i++; // add this
        }
}
}

Upvotes: 1

Antony Jack
Antony Jack

Reputation: 480

Hope it work...

public function uploadImages($data, $product,$color_id = null,$size_id = null)
{
        $previousImageIds = $product->images()->pluck('id');
        $i = 0;
        if (isset($data['images'])) {
            foreach ($data['images'] as $imageId => $image) {
              $file = 'images.' . $imageId;
                $dir = 'product/' . $product->id;
                if (str_contains($imageId, 'image_')) {
                    if (request()->hasFile($file)) {
                        // $i++; //removed
                        $this->create([
                                'path' => request()->file($file)->store($dir),
                                'product_id' => $product->id,
                                'product_color_id' => $color_id[$i],
                                'product_size_id'=> $size_id ,
                            ]);
                    }
                }
            $i++; //added
            } 
        }
}

Upvotes: 1

Related Questions