request = $request; $this->product = $product; $this->image = new ImageResize(); } /** * */ public function handle() { if ($this->request->colors) foreach ($this->request->colors as $color) { $id = $color['id'] == "null" ? null : $color['id']; $color_id = $color['color_id'] == "null" ? null : $color['color_id']; $sizes = !empty($color['sizes']) ? $color['sizes'] : null; if ($id == null) { $child = Product::create([ 'color_id' => $color_id, 'sizes' => $sizes, 'article_number' => $color['article_number'], 'child_id' => $this->product->id, //'published' => $this->product->published, //'available' => true ]); if (!empty($color['screens'])) { $this->uploadScreen($color['screens'], $child->id); } } else { $child = Product::find($id)->update([ 'color_id' => $color_id, 'sizes' => $sizes, 'article_number' => $color['article_number'], 'child_id' => $this->product->id, //'published' => $this->product->published, //'available' => true ]); if (!empty($color['screens'])) { $this->uploadScreen($color['screens'], $id); } } } } /** * @param $screens * @param $child_id */ private function uploadScreen($screens, $child_id) { if (!empty($screens)) { foreach ($screens as $screen) { if ($screen['id'] == 'undefined' || $screen['id'] == 'null' || $screen['id'] == null) { $folder = Carbon::now()->format('Y/m/d'); if ($screen['image']) { // 1. Store original (S3 if enabled) $path = $screen['image']->store("uploads/screens/{$folder}"); // 2. Local temp for resizing $tempPath = $screen['image']->store('temp', 'public'); $thumbPath = $this->image->resize($tempPath, 322, 'screens', true); Screen::create([ 'path' => $path, 'path_thumb' => $thumbPath, 'name' => basename($path), 'product_id' => $child_id, 'size' => Storage::disk(env('FILESYSTEM_DISK'))->size($path) ]); } } } } } }