如何针对不同的产品类型附上不同的图片

  • 本文关键字:类型 何针 php laravel
  • 更新时间 :
  • 英文 :


我正在建立一个电子商务网站,我想为同一产品添加不同的组合。我还使用jQuery添加了额外的输入字段。我可以成功插入与product_variation相关的所有数据,如颜色,重量,类型,数量,但图像除外。我的数据库表:products, product_images, product_variables。在product_variables表中,我有字段product_variation_image,我想用它来存储特定产品类型的图像。我只是不知道如何为特定的产品类型插入图像,是的,我知道我需要使用foreach循环,但问题是我已经使用foreach循环的产品变化数据,我尝试了很多不同的东西嵌套foreach到foreach,但似乎没有工作。ps product_images表我只用于产品基本图像,也用于没有任何类型的产品。我将在下面留下我的代码…对问题可能在哪里给出一些评论。

public function product_store(Request $request)
{
// PRODUCT TYPE VARIABLES //
$productTypes = $request->product_type;
$productColors = $request->product_color;
$productWeight = $request->product_weight;
$productQtys = $request->product_qty;
$productPrices = $request->product_variation_price;
$productTypeImages = $request->file('product_variation_image');
// PRODUCT TYPE VARIABLES ENDS //

// PRODUCT COVER IMAGE //
$productCoverImage = $request->file('product_cover_image');
$productCoverImageNewName = hexdec(uniqid()).'.'.$productCoverImage->getClientOriginalExtension();
Image::make($productCoverImage)->resize(917,1000)->save('upload/products/cover-images/'.$productCoverImageNewName);
$productCoverImageLink = 'upload/products/cover-images/'.$productCoverImageNewName;
// PRODUCT COVER IMAGE  ENDS //

if(!empty(implode($productTypes))) {
$productId = Products::insertGetId([
'category_id' => $request->category_id,
'sub_category_id' => $request->sub_category_id,
'sub_sub_category_id' => $request->sub_sub_category_id,
'product_name' => $request->product_name,
'product_link' => strtolower(str_replace(' ','-', $request->product_name)),
'product_desc' => $request->product_desc,
'product_short_desc' => $request->product_short_desc,
'product_price' => $request->product_price,
'product_discount' => $request->product_discount,
'product_cover_image' => $productCoverImageLink,
'new_product' => $request->new_product,
'featured' => $request->featured,
'special_offer' => $request->special_offer,
'status' => $request->status,
]);
// PRODUCT TYPE IMAGES //
foreach ($productTypeImages as $img) {
$productTypeImagesNewName = hexdec(uniqid()) . '.' . $img->getClientOriginalExtension();
Image::make($img)->resize(917, 1000)->save('upload/products/product-images/' . $productTypeImagesNewName);
$productTypesImagesLink = 'upload/products/product-images/' . $productTypeImagesNewName;
}
// PRODUCT TYPE IMAGES  ENDS //
foreach ($productTypes as $id => $key) {
ProductVariations::insert([
'product_id' => $productId,
'product_type' => $productTypes[$id],
'product_color' => $productColors[$id],
'product_weight' => $productWeight[$id],
//'product_variation_image' => $productTypesImagesLink[$id], <--- PROBLEM HERE
'product_variation_qty' => $productQtys[$id],
'product_variation_price' => $productPrices[$id],
'created_at' => Carbon::now()
]);
}
} else {
$productId = Products::insertGetId([
'category_id' => $request->category_id,
'sub_category_id' => $request->sub_category_id,
'sub_sub_category_id' => $request->sub_sub_category_id,
'product_name' => $request->product_name,
'product_link' => strtolower(str_replace(' ','-', $request->product_name)),
'product_desc' => $request->product_desc,
'product_short_desc' => $request->product_short_desc,
'product_price' => $request->product_price,
'product_discount' => $request->product_discount,
'product_cover_image' => $productCoverImageLink,
'new_product' => $request->new_product,
'featured' => $request->featured,
'special_offer' => $request->special_offer,
'status' => $request->status,
'created_at' => Carbon::now()
]);
}


// PRODUCT MULTIPLE IMAGES //
$productImages = $request->file('product_image');
foreach ($productImages as $productImage) {
$productImagesNewName = hexdec(uniqid()).'.'.$productImage->getClientOriginalExtension();
Image::make($productImage)->resize(917,1000)->save('upload/products/product-images/'.$productImagesNewName);
$productImagesLink = 'upload/products/product-images/'.$productImagesNewName;
ProductImages::insert([
'product_id' => $productId,
'product_image' => $productImagesLink,
'created_at' => Carbon::now()
]);
}
// PRODUCT MULTIPLE IMAGES ENDS //
$notification = array(
'message' => 'Product Inserted!',
'alert-type' => 'success'
);
return redirect()->back()->with($notification);
}

我有一个不同的解决方案。

在插入带有多个图像的产品后,如果产品有任何变化,我将用户重定向到另一个页面。如果他愿意,他可以为每种类型选择图像。我只从product_images表中插入图像id,到product_variables表字段- product_variation_image.

顺便说一下,我使用了一个名为image-picker的jQuery插件。现在一切正常,我的客户很满意。

产品功能:

public function product_store(Request $request)
{
// PRODUCT TYPE VARIABLES //
$productTypes = $request->product_type;
$productColors = $request->product_color;
$productWeight = $request->product_weight;
$productQtys = $request->product_qty;
$productPrices = $request->product_variation_price;
$productTypeImages = $request->file('product_variation_image');
// PRODUCT TYPE VARIABLES ENDS //
// PRODUCT COVER IMAGE //
$productCoverImage = $request->file('product_cover_image');
if($productCoverImage) {
$productCoverImageNewName = hexdec(uniqid()) . '.' . $productCoverImage->getClientOriginalExtension();
Image::make($productCoverImage)->resize(917, 1000)->save('upload/products/cover-images/' . $productCoverImageNewName);
$productCoverImageLink = 'upload/products/cover-images/' . $productCoverImageNewName;
} else {
$productCoverImageLink = 'upload/no-image/image.png';
}
// PRODUCT COVER IMAGE  ENDS //
if(!empty(implode($productTypes))) {
$productId = Products::insertGetId([
'category_id' => $request->category_id,
'sub_category_id' => $request->sub_category_id,
'sub_sub_category_id' => $request->sub_sub_category_id,
'product_name' => $request->product_name,
'product_link' => strtolower(str_replace(' ','-', $request->product_name)),
'product_desc' => $request->product_desc,
'product_short_desc' => $request->product_short_desc,
'product_price' => $request->product_price,
'product_discount' => $request->product_discount,
'product_cover_image' => $productCoverImageLink,
'new_product' => $request->new_product,
'featured' => $request->featured,
'special_offer' => $request->special_offer,
'status' => $request->status,
]);
foreach ($productTypes as $id => $key) {
ProductVariations::insert([
'product_id' => $productId,
'product_type' => $productTypes[$id],
'product_color' => $productColors[$id],
'product_weight' => $productWeight[$id],
'product_variation_qty' => $productQtys[$id],
'product_variation_price' => $productPrices[$id],
'created_at' => Carbon::now()
]);
}
} else {
$productId = Products::insertGetId([
'category_id' => $request->category_id,
'sub_category_id' => $request->sub_category_id,
'sub_sub_category_id' => $request->sub_sub_category_id,
'product_name' => $request->product_name,
'product_link' => strtolower(str_replace(' ','-', $request->product_name)),
'product_desc' => $request->product_desc,
'product_short_desc' => $request->product_short_desc,
'product_price' => $request->product_price,
'product_discount' => $request->product_discount,
'product_cover_image' => $productCoverImageLink,
'new_product' => $request->new_product,
'featured' => $request->featured,
'special_offer' => $request->special_offer,
'status' => $request->status,
'created_at' => Carbon::now()
]);
}
// PRODUCT MULTIPLE IMAGES //
$productImages = $request->file('product_image');
if($productImages) {
foreach ($productImages as $productImage) {
$productImagesNewName = hexdec(uniqid()) . '.' . $productImage->getClientOriginalExtension();
Image::make($productImage)->resize(917, 1000)->save('upload/products/product-images/' . $productImagesNewName);
$productImagesLink = 'upload/products/product-images/' . $productImagesNewName;
ProductImages::insert([
'product_id' => $productId,
'product_image' => $productImagesLink,
'created_at' => Carbon::now()
]);
}
}
// PRODUCT MULTIPLE IMAGES ENDS //
$notification = array(
'message' => 'Product Inserted!',
'alert-type' => 'success'
);
if(!empty(implode($productTypes))) {
return redirect()->route('admin.product-variations-images-settings', ['id' => $productId])->with($notification);
} else {
return redirect()->route('admin.products')->with($notification);
}
}

图像变化函数:

public function product_variations_images_settings($id)
{
$productVariations = ProductVariations::where('product_id', $id)->get();
$productImages = ProductImages::where('product_id', $id)->get();
return view('administrator.pages.products.product-variations', compact('productVariations','productImages'));
}
public function product_variations_images_store(Request $request)
{
$productVariationId = $request->id;
$productImageId = $request->product_variation_image;
$array = array_combine($productVariationId, $productImageId);
foreach ($array as $id => $key){
//dd($key);
ProductVariations::findOrFail($id)->update([
'product_variation_image' => $key,
'updated_at' => Carbon::now()
]);
}
$notification = array(
'message' => 'Images Set!',
'alert-type' => 'success'
);
return redirect()->route('admin.products')->with($notification);
}

相关内容

  • 没有找到相关文章

最新更新