donkey199024 2019-07-08 16:12
浏览 202

提交数据时Laravel错误未定义索引:

I'm trying to add data to my cart table by adding a product to cart but instead i get the above error Undefined index:size

code from the responsible controller

 public function addToCart(Request $request){
        Session::forget('CouponAmount');
        Session::forget('CouponCode');
        $data = $request->all();

        if(empty($data['user_email'])){
            $data['user_email']='';
        }
        $session_id= Session::get('session_id');
        if(empty($session_id)){
            $session_id = str_random(40);
            Session::put('session_id', $session_id);
        }

        $sizeArr=explode("-",$data['size']);

        $countProducts = DB::table('cart')- 
        >where(['product_id'=>$data['product_id'], 
        'product_color'=>$data['product_color'],
        'size'=>$sizeArr[1], 'session_id'=>$session_id,])->count();

        if($countProducts>0){
            return redirect()->back()->with('flash_message_error', 'Exact 
            product already exists in Cart!');

        }else {
            $getSKU = ProductsAttribute::select('sku')- 
            >where(['product_id'=>$data['product_id'], 'size'=>$sizeArr[1]])- 
            >first();

            DB::table('cart')->insert(['product_id'=>$data['product_id'], 
            'product_name'=>$data['product_name'], 
            'product_code'=>$getSKU->sku, 
            'product_color'=>$data['product_color'], 'price'=>$data['price'],
            'size'=>$sizeArr[1], 'quantity'=>$data['quantity'], 
            'user_email'=>$data['user_email'], 'session_id'=>$session_id,]);
        }

        return redirect('cart')->with('flash_message_success', 'Product has 
        been added to cart!');
    }

the blade file where size is input

              <h1>{{ $productDetails->product_name }}</h1>
                        <p>{{ $productDetails->description }}</p>
                         <div class="dropdown_top">
                            <div class="dropdown_left">
                             <select id="size" class="dropdown" tabindex="10" 
                              data-settings='{"wrapperClass":"metro1"}'>
                                <option value="0">Select size</option>  
                                @foreach ($productDetails->attributes as 
                                $sizes)
                                    <option value="{{ $productDetails->id }}- 
                                     {{ $sizes->size }}">{{ $sizes->size }} 
                                    </option>
                                @endforeach
                             </select>
                            </div>

instead of being redirected to my cart page I get Undefined index:size

  • 写回答

1条回答 默认 最新

  • donglv7097 2019-07-08 16:30
    关注

    I added this in a comment, but thought I'd eloborate :)

    The $request->all() call isn't returning 'size' because the name attribute is missing from your <select> tag in the template. The following should fix it:

    <select name="size" id="size" class="dropdown" tabindex="10" data-settings='{"wrapperClass":"metro1"}'>

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度