根据条件

时间:2018-02-12 03:43:34

标签: php laravel

我已经制作了更新表单,以便在2个条件下获取数据

  1. 当用户设置产品的新数量
  2. 当用户应用优惠券代码
  3. 他们应该并排工作,相互打断, LOGIC 是:

    1. 如果用户更改了quantity,请将其更新
    2. 如果用户添加coupon,请将其应用
    3. 如果用户更改quantity and add coupon,请同时更新。
    4. 问题

      1. 我的代码works,如果我更改数量。
      2. 我的代码doesn't work优惠券更新。
      3. 我的代码doesn't work,两个输入在一起。
      4. 我在更改数量时获得2 sessions alert
      5.   

        PS:我的问题是参考我的if陈述。 (基本上我需要帮助来修复我的if's

        public function update(Request $request, $id)
        {
        
          //list of coupons
          $coupons = Coupon::all();
          //get current time
          $mytime = Carbon::now();
        
          $qty = $request->input('quantity');
          $productId = $request->proId;
          $products = Product::where('id', $productId)->firstOrFail();
          $stock = $products->stock;
        
        
          //coupons
            $couponcode = $request->input('coupon');
            $catId = Coupon::where('category_id', $products->category_id)
                            ->where('value_to', '>=', $mytime)
                            ->when($couponcode, function ($query) use ($couponcode) {
                              return $query->where('title', $couponcode);
                            })
                            ->first();
            if($catId == $couponcode){ //first if
              $coupon = new \Darryldecode\Cart\CartCondition(array(
                'name' => $catId->title,
                'type' => 'coupon',
                'target' => 'item',
                'value' => -$catId->amount,
              ));
            }else{
              Session::flash('danger', 'Your request cannot be handle, please try again!');
            }
          //end coupons
        
          if($qty<$stock && empty($couponcode)){ //second if
        
            Cart::update($id, array(
              'quantity' => array(
                  'relative' => false,
                  'value' => $request->input('quantity'),
              ),
            ));
            Session::flash('success', 'Cart is updated.');
            return redirect()->route('cart.index');
        
          }elseif(!empty($couponcode) && $catId == $couponcode){ //third if
            Cart::update($id, array(
              'conditions' => $coupon,
            ));
            Session::flash('success', 'Coupon applied successfully.');
            return redirect()->route('cart.index');
          }else{ // fourth if
            Session::flash('danger', 'Your request cannot be handle, please try again!');
            return redirect()->route('cart.index');
          }
        }
        

        截图

        screen1

        任何人都可以提供帮助吗?

2 个答案:

答案 0 :(得分:0)

当你创建$catId时,你会得到一个雄辩的模型,因为你使用first()

$ catId =优惠券::其中(&#39; category_id&#39;,$ products-&gt; category_id)                      - &gt; where(&#39; value_to&#39;,&#39;&gt; =&#39;,$ mytime)                      - &gt; when($ couponcode,function($ query)use($ couponcode){                       返回$ query-&gt; where(&#39; title&#39;,$ couponcode);                     })                      - &gt;首先();

然后你比较它:

 if($catId == $couponcode)

永远不会是真的

解决方案是从查询中正确获取所需的值:

...->first()->column_name;

答案 1 :(得分:0)

您应该在条件 s 中使用优惠券代码$catId->title$catId->category_id或其他任何内容)的列名:< / p>

if($catId->title == $couponcode){ //first if
  $coupon = new \Darryldecode\Cart\CartCondition(array(
    'name' => $catId->title,
    'type' => 'coupon',
    'target' => 'item',
    'value' => $catId->amount,
  ));
}else{
  Session::flash('danger', 'Your request cannot be handle, please try again!');
}

同样在:

elseif(!empty($couponcode) && $catId->title == $couponcode){ //third if

<强>更新
您应该将if块重新排列为:
1。如果同时给出(数量和代码)   嵌套&gt; 如果数量小于库存且代码正确update the cart
2。如果仅提供数量update quantity
3。如果只是优惠券apply coupon 4. 如果没有给出do nothing

// quantity
$qty = $request->input('quantity');
$products = Product::findOrFail($request->proId);
$stock = $products->stock;

// coupons
$couponcode = $request->input('coupon');
$catId = Coupon::where('category_id', $products->category_id)
               ->where('value_to', '>=', $mytime)
               ->when($couponcode, function ($query) use ($couponcode) {
                    return $query->where('title', $couponcode);
                })
               ->first();

if (!empty($qty) && !empty($couponcode)) { // 1. if both (quantity and couponcode) are given
    if ($qty < $stock && (!is_null($catId) && $catId->title == $couponcode)) { // if quantity < stock AND coupon-code is correct

        $coupon = new \Darryldecode\Cart\CartCondition(array(
            'name' => $catId->title,
            'type' => 'coupon',
            'target' => 'item',
            'value' => $catId->amount,
        ));

        Cart::update($id, array(
            'quantity' => array(
                'relative' => false,
                'value' => $qty,
            ),
            'conditions' => $coupon,
        ));

        Session::flash('success', 'Cart updated.');
        return redirect()->route('cart.index');

    } elseif ($qty > $stock) {
        Session::flash('danger', 'quantity not available!');
        return redirect()->route('cart.index');
    } else {
        Session::flash('danger', 'invalid coupon code!');
        return redirect()->route('cart.index');
    }
} elseif (!empty($qty)) { // 2. if just quantity is given   
    if ($qty < $stock) {

        Cart::update($id, array(
            'quantity' => array(
                'relative' => false,
                'value' => $qty,
            ),
        ));

        Session::flash('success', 'Cart updated.');
        return redirect()->route('cart.index');

    } else {
        Session::flash('danger', 'quantity not available!');
        return redirect()->route('cart.index');
    }
} elseif (!empty($couponcode)) { // 3. if just couponcode is given
    if (!is_null($catId) && $catId->title == $couponcode) {

        $coupon = new \Darryldecode\Cart\CartCondition(array(
            'name' => $catId->title,
            'type' => 'coupon',
            'target' => 'item',
            'value' => $catId->amount,
        ));

        Cart::update($id, array(
            'conditions' => $coupon,
        ));

        Session::flash('success', 'Coupon applied successfully.');
        return redirect()->route('cart.index');

    } else {
        Session::flash('danger', 'invalid coupon code!');
        return redirect()->route('cart.index');
    }
} else{ // 1. if nothing is given
    Session::flash('danger', 'Your request cannot be handle, please try again!');
    return redirect()->route('cart.index');
}