Update Inner Child Inside Child Inside Parent In laravel 10
Hi , i built a small ecommerce using laravel and i am using React for front-end my question about the product store and update, every thing is work but there some problems in, i am creating the product using the store method :
public function store(StoreProductRequest $request)
{
$product = new Product();
$request->validated();
$productCreated = $product->create([
'title_en' => $request->title_en,
'title_ar' => $request->title_ar,
'description_en' => $request->description_en,
'description_ar' => $request->description_ar,
'price' => $request->price,
'discount' => $request->discount,
'price_suadia' => $request->price_suadia,
'discount_suadia' => $request->discount_suadia,
'price_emarat' => $request->price_emarat,
'discount_emarat' => $request->discount_emarat,
'price_dollar' => $request->price_dollar,
'discount_dollar' => $request->discount_dollar,
'coming_soon' => $request->coming_soon,
]);
if ($request->colors) {
foreach ($request->colors as $colorData) {
$color = new ProductColor();
$color->name_ar = $colorData['name_ar'];
$color->name_en = $colorData['name_en'];
$color->color = $colorData['color'];
$color->quantity = $colorData['quantity'];
$color->product_id = $productCreated->id;
$file = $colorData['image'];
if (filter_var($file, FILTER_VALIDATE_URL)) {
$color->url = $file;
} else {
$filename = date('YmdHis') . '.' . $file->getClientOriginalExtension();
$path = 'images';
$file->move($path, $filename);
$file = url('/') . '/images/' . $filename;
$color->url = $file;
}
$color->save();
if (isset($colorData['images'])) {
$files = $colorData['images'];
$i = 0;
foreach ($files as $file) {
$i = $i + 1;
$image = new ProductImage();
$image->product_color_id = $color->id;
if (filter_var($file, FILTER_VALIDATE_URL)) {
$image->url = $file;
} else {
$filename = date('YmdHis') . $i . '.' . $file->getClientOriginalExtension();
$path = 'images';
$file->move($path, $filename);
$image->url = url('/') . '/images/' . $filename;
}
$image->save();
}
}
}
} else {
return response()->json([
'error' => 'يا أخي على الاقل لون واحد'
]);;
}
return $productCreated;
}
the product has all this columns, but has color child which is has a child also called images every color has this data:
$color = new ProductColor();
$color->name_ar = $colorData['name_ar'];
$color->name_en = $colorData['name_en'];
$color->color = $colorData['color'];
$color->quantity = $colorData['quantity'];
$color->product_id = $productCreated->id;
$file = $colorData['image'];
if (filter_var($file, FILTER_VALIDATE_URL)) {
$color->url = $file;
} else {
$filename = date('YmdHis') . '.' . $file->getClientOriginalExtension();
$path = 'images';
$file->move($path, $filename);
$file = url('/') . '/images/' . $filename;
$color->url = $file;
}
and the color also has this :
$color->save();
if (isset($colorData['images'])) {
$files = $colorData['images'];
$i = 0;
foreach ($files as $file) {
$i = $i + 1;
$image = new ProductImage();
$image->product_color_id = $color->id;
if (filter_var($file, FILTER_VALIDATE_URL)) {
$image->url = $file;
} else {
$filename = date('YmdHis') . $i . '.' . $file->getClientOriginalExtension();
$path = 'images';
$file->move($path, $filename);
$image->url = url('/') . '/images/' . $filename;
}
$image->save();
}
}
everything works fine but the problem when i update the product with its images:
public function update(StoreProductRequest $request, $id)
{
$product = Product::findOrFail($id);
$request->validated();
$product->update([
'title_en' => $request->title_en,
'title_ar' => $request->title_ar,
'description_en' => $request->description_en,
'description_ar' => $request->description_ar,
'price' => $request->price,
'discount' => $request->discount,
'price_suadia' => $request->price_suadia,
'discount_suadia' => $request->discount_suadia,
'price_emarat' => $request->price_emarat,
'discount_emarat' => $request->discount_emarat,
'price_dollar' => $request->price_dollar,
'discount_dollar' => $request->discount_dollar,
'coming_soon' => $request->coming_soon
]);
// Update colors and images
$product->colors()->delete();
if ($request->colors) {
foreach ($request->colors as $colorData) {
$color = new ProductColor();
$color->name_ar = $colorData['name_ar'];
$color->name_en = $colorData['name_en'];
$color->color = $colorData['color'];
$color->quantity = $colorData['quantity'];
$color->product_id = $product->id;
// Handle color image - URL or file
if (isset($colorData['image'])) {
$image = $colorData['image'];
if (filter_var($image, FILTER_VALIDATE_URL)) {
$color->url = $image;
} else {
$filename = date('YmdHis') . '.' . $image->getClientOriginalExtension();
$path = 'images';
$image->move($path, $filename);
$color->url = url('/') . '/images/' . $filename;
}
}
$color->save();
// Handle additional images - URLs or files
if (isset($colorData['images'])) {
foreach ($colorData['images'] as $image) {
$productImage = new ProductImage();
$productImage->product_color_id = $color->id;
if (filter_var($image, FILTER_VALIDATE_URL)) {
$productImage->url = $image;
} else {
$filename = date('YmdHis') . '_' . uniqid() . '.' . $image->getClientOriginalExtension();
$path = 'images';
$image->move($path, $filename);
$productImage->url = url('/') . '/images/' . $filename;
}
$productImage->save();
}
}
}
} else {
return response()->json([
'error' => 'يا أخي على الأقل لون واحد'
]);
}
return response()->json(['data' => 'Product Updated Succesfully']);
}
as you can see i am deleting the colors and re create it , the problem now : when the user update the product everything works fine , except for images of the color, if he upload five images it become 3 images in the database, and maybe 2 i don't know why and there is another problem, in my way everytime the product updates , it is create a new color is that problem for perfomance ? so does anyone know why first problem happens ?
Please or to participate in this conversation.