php - Laravel 函数 Controller 验证错误

标签 php laravel

当我尝试应用优惠券代码时遇到验证问题。 我必须满足 4 条件

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

下面是该功能的 Controller 部分:-

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>

最佳答案

要检查已使用的优惠券,最好使用存在而不是计数

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

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

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()
    ];
}

Jquery:

$(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);
                }

            }
        })
    });
});

关于php - Laravel 函数 Controller 验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53333029/

相关文章:

php - 如何让nginx显示错误日志?

laravel - 如何从 Laravel Controller 返回 AJAX 错误?

php - 在 Laravel 5.3 中构建链接的正确方法

php - 在elasticsearch中是否可以只使用过滤器而不使用文本搜索

php - 调用未定义函数 mysql_connect()

php - 如何在 Laravel-5 中构建子查询

php - 使用 PHP 接收 JSON POST

php - PDO - 在一个查询中进行多次插入/更新

PHP Session 类和 $_SESSION 数组

php - Wordpress 短代码不起作用