首页 > 解决方案 > Laravel 函数控制器验证错误

问题描述

我尝试应用优惠券代码时遇到验证问题。我必须申请4个条件

  1. 如果我从数据库中应用了正确的优惠券,那就没问题了。
  2. 如果我再次从数据库中应用了正确的优惠券,则会显示消息“此优惠券已使用”。
  3. 如果我只是直接输入按钮(空白值),它将显示消息“请插入优惠券代码
  4. 但问题是我输入了错误的优惠券代码,我需要显示“输入了错误的优惠券代码”消息

以下是该功能的控制器部分:-

public function checkCoupon(Request $res)
{
    $code = $res->code;
    $check = DB::table('coupons')
        ->where('coupon_code',$code)
        ->get();
    if(count($check)=="1") {
        $user_id = Auth::user()->id;
        $check_used = DB::table('used_coupons')
            ->where('user_id', $user_id)
            ->where('coupon_id', $check[0]->id)
            ->count();

        if($check_used=="0"){
            $used_add = DB::table('used_coupons')
                ->insert([
                    'coupon_id' => $check[0]->id,
                    'user_id' => $user_id
                ]);
            $insert_cart_total = DB::table('cart_total')
                ->insert([
                    'cart_total' => Cart::total(),
                    'discount' => $check[0]->discount,
                    'user_id' => $user_id,
                    'gtotal' =>  Cart::total() - (Cart::total() * $check[0]->discount)/100,
                ]);
        }
        else{
        ?>
            <div class="alert alert-warning">This Coupon Have Already Used</div>
        <?php
        }
    }
    else if($code==$check){
        ?>
        <div class="alert alert-danger">Wrong Coupon Code Entered</div>
    <?php }
    else{
        ?>
        <div class="alert alert-danger">Please Insert The Coupon Code </div>
    <?php }
}

申请优惠券功能按钮 的JS文件:-

$(document).ready(function(){           
 	$('#coupon_btn').click(function(){
                var coupon_id = $('#coupon_id').val();
                $.ajax({
                    url:'{{url('/checkCoupon')}}',
                    data: 'code=' + coupon_id,
                    success:function(res){
                        $('#cartTotal').html(res);
                    }
                })
            });
        });

查看文件

         <div class="cart-total" >
                <h4>Total Amount</h4>
                <table>
                    <tbody>
                    <tr>
                        <td>Sub Total</td>
                        <td>$ <?php echo Cart::subtotal(); ?></td>
                    </tr>
                    <tr>
                        <td>Tax (%)</td>
                        <td>$ <?php echo Cart::tax(); ?></td>
                    </tr>


                    <tr>
                        <td>Grand Total new</td>
                        <td>$ <?php echo Cart::total(); ?></td>
                    </tr>

                    <tr>
                        <td>Discount(%) </td>
                        <td> <?php echo $disnew; ?></td>
                    </tr>

                    <tr>
                        <td>Grand Total (After discount) </td>
                        <td>$ <?php echo $gtnew; ?></td>
                    </tr>
                    </tbody>
                </table>
                <input type="submit" class="btn update btn-block" style="color: white;font-weight: bold;" value="Continue Shopping">
                <a href="<?php echo url('checkout') ?>" class="btn check_out btn-block"  style="color: white;font-weight: bold;">Checkout</a>
            </div>

标签: phplaravel

解决方案


要检查使用过的优惠券,最好使用exists而不是count

为了更好地使用优惠券而不是获取(首先- 返回第一个元素,您不需要在集合上使用索引)

我认为,在这种情况下最好使用回报。代码和逻辑看起来很清楚。

public function checkCoupon(Request $res)
{
    $code = $res->input('code');
    $userId = Auth::user()->id;

    if(!$code){
        return ['error' => '<div class="alert alert-danger">Please Insert The Coupon Code </div>'];
    }

    $coupon = DB::table('coupons')
            ->where('coupon_code', $code)
            ->first();

    if(!$coupon) {
        return ['error' => '<div class="alert alert-danger">Wrong Coupon Code Entered</div>'];
    }

    $isUsed = DB::table('used_coupons')
            ->where('user_id', $userId)
            ->where('coupon_id', $coupon->id)
            ->exists();

    if($isUsed){
        return ['error' => '<div class="alert alert-warning">This Coupon Have Already Used</div>'];
    }

    DB::table('used_coupons')
        ->insert([
            'coupon_id' => $coupon->id,
            'user_id' => $userId
        ]);

    DB::table('cart_total')
        ->insert([
            'cart_total' => Cart::total(),
            'discount' => $coupon->discount,
            'user_id' => $userId,
            'gtotal' =>  Cart::total() - (Cart::total() * $coupon->discount)/100,
    ]);

    return [
        'subtotal' => Cart::subtotal(),
        'total' => Cart::total()
    ];
}

查询:

$(document).ready(function(){           
    $('#coupon_btn').click(function(){
        var coupon_id = $('#coupon_id').val();
        $.ajax({
            url:'{{url('/checkCoupon')}}',
            dataType: "json",
            data: {code: coupon_id},
            success:function(res){
                if(res.error){
                    $('...error_selector...').html(res.error);
                } else {
                    $('...total_selector...').html(res.total);
                    $('...subtotal_selector...').html(res.subtotal);
                }

            }
        })
    });
});

推荐阅读