public static function boot() { parent::boot(); ProductColor::deleting(function ($productColor) { File::delete($productColor->image); }); }
public function getProductImageByColor($id) { return ProductColor::find($id); }
/** * @param Request $request * @param $product */ private function saveProduct(Request $request, $product) { $product->fill($request->input()); $productColorIds = []; if ($request->hasFile('image')) { $fileName = str_random(32) . '.' . $request->file('image')->getClientOriginalExtension(); $request->file('image')->move(public_path() . '/img/uploads/', $fileName); $product->image = 'img/uploads/' . $fileName; } $productTags = $request->input('product_tags'); $productTagsIds = []; if ($productTags) { foreach ($productTags as $productTag) { $productTagDb = ProductTag::where('name', $productTag)->first(); if ($productTagDb == null) { $productTagDb = ProductTag::create(['name' => $productTag]); } array_push($productTagsIds, $productTagDb->id); } } $product->save(); $product->productTags()->sync($productTagsIds); if ($request->input('product-color-id') != null) { foreach ($request->input('product-color-id') as $key => $productColorId) { $productColor = ProductColor::findOrNew($productColorId); $productColorImage = $request->file('product-color-image')[$key]; if ($productColorImage != null) { $productColorImageFileName = str_random(32) . '.' . $productColorImage->getClientOriginalExtension(); $productColorImage->move(public_path() . '/img/uploads/', $productColorImageFileName); $productColor->image = 'img/uploads/' . $productColorImageFileName; } $productColorProductImage = $request->file('product-color-product-image')[$key]; if ($productColorProductImage != null) { $productColorProductImageFileName = str_random(32) . '.' . $productColorProductImage->getClientOriginalExtension(); $productColorProductImage->move(public_path() . '/img/uploads/', $productColorProductImageFileName); $productColor->product_image = 'img/uploads/' . $productColorProductImageFileName; } $productColor->product_id = $product->id; $productColor->code = $request->input('product-color-code')[$key]; $productColor->title = $request->input('product-color-title')[$key]; $productColor->shade = $request->input('product-color-shade')[$key]; $productColor->save(); if ($productColorId != "") { array_push($productColorIds, $productColorId); } else { array_push($productColorIds, $productColor->id); } } } foreach ($product->productColors as $productColor) { if (!in_array($productColor->id, $productColorIds)) { $productColor->delete(); } } $productGallery = $product->productGalleries()->first(); if ($productGallery == null) { $productGallery = new ProductGallery(); $productGallery = $product->productGalleries()->save($productGallery); } $productGallery->productGalleryItems()->delete(); if ($request->input('product_id') != null) { foreach ($request->input('product_id') as $recommendedProductId) { $productGallery->productGalleryItems()->save(new ProductGalleryItem(['product_id' => $recommendedProductId])); } } }