我提供的代码用于保存每个产品变体的产品信息和图像。我面临的问题是,相同的图像被保存为每个变体,而你希望每个产品变体在每种颜色的唯一图像。
' $product_code = uniqid('#');
$product = new Product;
$product->brand_id = $request->brand_id;
$product->category_id = $request->category_id;
$product->subcategory_id = $request->subcategory_id;
$product->subsubcategory_id = $request->subsubcategory_id;
$product->product_name_en = $request->product_name_en;
$product->product_name_ar = $request->product_name_ar;
$product->product_slug_en = strtolower(str_replace(' ', '-', $request->product_name_en));
$product->product_slug_ar = str_replace(' ', '-', $request->product_name_ar);
$product->product_code = $product_code;
$product->product_tags_en = $request->product_tags_en;
$product->product_tags_ar = $request->product_tags_ar;
$product->product_weight = $request->product_weight;
$product->short_descp_en = $request->short_descp_en;
$product->short_descp_ar = $request->short_descp_ar;
$product->long_descp_en = $request->long_descp_en;
$product->long_descp_ar = $request->long_descp_ar;
$product->hot_deals = $request->hot_deals;
$product->featured = $request->featured;
$product->special_offer = $request->special_offer;
$product->special_deals = $request->special_deals;
$product->product_thumbnail = $save_url;
$product->save();
$product_id = $product->id;
// create an array to store all the images for each variant
$variantImages = [];
foreach ($request->color_en as $i => $color) {
$variantData = [
'color_en' => $color,
'size_en' => $request->size_en[$i],
'size_ar' => $request->size_ar[$i],
'selling_price' => $request->selling_price[$i],
'discount_price' => $request->discount_price[$i],
'product_qty' => $request->product_qty[$i],
];
// Create a new variant
$variant = new ProductVariant;
$variant->product_id = $product_id;
$variant->color_en = $variantData['color_en'];
$variant->size_en = $variantData['size_en'];
$variant->size_ar = $variantData['size_ar'];
$variant->selling_price = $variantData['selling_price'];
$variant->discount_price = $variantData['discount_price'];
$variant->product_qty = $variantData['product_qty'];
$variant->save();
if ($request->hasFile('multi_img')) {
$images = $request->file('multi_img');
$variantImages[$i] = []; // create an array to store the images for this variant
foreach ($images as $img) {
$name_gen = uniqid() . '_' . time() . '_' . $variant->id . '_' . $i . '.' . $img->getClientOriginalExtension();
$save_dir = public_path('upload/products/multi-image');
if (!file_exists($save_dir)) {
mkdir($save_dir, 0777, true);
}
if (is_writable($save_dir)) {
$photo = Image::make($img);
// resize and save the image
$photo->resize(800, null, function ($constraint) {
$constraint->aspectRatio();
});
$photo->save($save_dir . '/' . $name_gen);
$save_url = 'upload/products/multi-image/' . $name_gen;
}
// create the image record and save its variant id
// $multiImage = new ProductVariantImage;
// $multiImage->product_variant_id = $variant->id;
// $multiImage->photo_name = $save_url;
// $multiImage->save();
// add the image path to the array for this variant
$variantImages[$i][] = [
'photo_name' => $save_url,
'product_variant_id' => $variant->id,
];
}
// ProductVariantImage::insert($variantImages[$i]);
}
}
// dd($variantImages);
foreach ($variantImages as $i => $images) {
foreach ($images as $multiImage) {
$multiImage = new ProductVariantImage;
$multiImage->product_variant_id = $variant->id;
$multiImage->photo_name = $$save_url;
$multiImage->save();
}
}`
我希望每种颜色的每个产品变体都有独特的图像。
你的代码有些地方不对。试试改成:
//...
foreach ($variantImages as $i => $images) {
foreach ($images as $image) {
$multiImage = new ProductVariantImage;
$multiImage->product_variant_id = $image['product_variant_id'];
$multiImage->photo_name = $image['photo_name'];
$multiImage->save();
}
}
//...