$random_sku= Str::random(10);
$productDetails= Product::with('attributes')->where(['id'=>$id])->first();
//$productsDetails= json_decode(json_encode($productsDetails));
if($request->isMethod('post')){
$data=$request->all();
foreach($data['product_sku'] as $key=>$value){
if(!empty($value)){
//Prevent SKU Duplication SKU Check
$attCountSKUSeller=ProductAttributes::where('product_sku',$value)->count();
if($attCountSKUSeller>0){
return redirect('/seller-add-product-attributes/'.$id)->with('flash_message_error','product SKU already exists, please add another Product SKU!');
}
//Prevent Size Duplication Check
$attrcountSizesSeller= ProductAttributes::where(['product_id'=>$id, 'product_size'=>$data['product_size'][$key]])->count();
if($attrcountSizesSeller>0){
return redirect('/seller-add-product-attributes/'.$id)->with('flash_message_error','"'.$data['product_size'][$key].'" Size already exists for this product, please add another Size!');
}
$attribute=new ProductAttributes;
$attribute->product_id=$id;
$attribute->product_sku=$value;
$attribute->product_size=$data['product_size'][$key];
$attribute->product_price=$data['product_price'][$key];
$attribute->product_colour=$data['product_colour'][$key];
$attribute->product_stock=$data['product_stock'][$key];
$attribute->save();
}
}
return redirect('/seller-add-product-attributes/'.$id)->with('flash_message_success','Product(s) Attribute(s) have been Added Successfully');
}
return view('shop.seller_product_attributes')->with(compact('productDetails', 'random_sku'));
//return view('shop.seller_product_attributes');
}
pls, guys help me with this problem I have tried numerous laravel helper function like create(); insert();.I don’t know what i am doing wrong and if there is better way of doing this help me.these is my view:
Price
<div class="col-md-2 col-sm-2 col-lg-2 col-xs-2">
<label for="product_size">Size</label>
<select class="browser-default custom-select custom-select mb-3" name="product_size[]" id="product_size" >
<option selected disabled>Select Size</option>
<option>Small</option>
<option>Big</option>
<option>Extra Large</option>
</select>
</div>
<div class="col-md-2 col-sm-2 col-lg-2 col-xs-2">
<label for="product_colour">Colour</label>
<input type="text" name="product_colour[]" id="product_colour" placeholder="Colour" class="form-control mb-3" validate="name" >
</div>
<div class="col-md-2 col-sm-2 col-lg-2 col-xs-2">
<label for="product_stock">Stock</label>
<input type="number" name="product_stock[]" id="product_stock" placeholder="Quantity" class="form-control mb-3" >
</div>
<div class="button-group mt-4 my-2">
<a href="javascript:void(0)" class="btn btn-primary btn-sm" id="plus5">Add More</a>
<a href="javascript:void(0)" class="btn btn-danger btn-sm" id="minus5">Remove</a>
</div>
</div>
</div>
<div class="row d-flex justify-content-center ">
<button type="submit" class="btn btn-primary btn-block text-center mb-4 mx-2">Submit</button>
</div>
</form>
what I am trying to do is multiple insertions