在laravel 5.8中插入多个文本字段时未定义索引

时间:2019-08-28 12:35:36

标签: laravel laravel-5.8

laravel 5.8中多个文本字段插入中的未定义索引

我试图一次向mysql表中插入多个数据,但是每次我单击提交按钮时laravel一直告诉我未定义索引:product_sku.i已​​经尝试了多种方法来解决问题,但我无法解决。 / p>

这是我的控制器

$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');

这是我的观点:

<form class="card-body" action="{{url('/seller-add-product-attributes/'.$productDetails->id)}}" method="post">
    <input type="hidden" name="_token" value="{{csrf_token()}}">
    <div class="form-group" id="dynamic_form">
        <div class="row">
            <!-- <input type="hidden" name="product_sku" id="product_sku"  value="{{$random_sku}}">-->
            <div class="col-md-2 col-sm-2 col-lg-2 col-xs-2">
                <label for="product_price">Price</label>
                <input type="text" name="product_price[]" id="product_price" placeholder="Price" class="form-control mb-3" validate="number" >
            </div>
            <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>

我已经尝试了很多laravel辅助函数来完成此操作,例如create()使用大量可分配的insert()。请问,我想知道如何正确地执行此操作,因为我从事此工作已有4天了任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

这是一个相当简单的问题。您的视图中没有任何product_sku输入(已被注释掉)。