why this function don't pass the coupon data to view in PHP Laravel?

Viewed 93

I have created an online store, but there is a problem that when I enter the discount code in, the condition is executed correctly and returns a message of success of the operation, but no data for the entered discount code is returned.

the check function

public function coupon_check()
    {
        $coupon = Coupon::where('code', request()->post('code'))->first();

        if (isset($coupon) || !empty($coupon)) {
            toast('Successfully !!!', 'success');
            return redirect()->back()->with(['coupon'=>$coupon]);
        } else {
            toast('Not Found !!!', 'error');
            return redirect()->back();
        }
    }

in here

            <tr> 
               <td style="color: white">{{__('discount')}}</td>
               <td style="color: white">@if($coupon){{$coupon->discount_value }} 
                 @endif </td> 
            </tr>

when executing this form

       <form action="{{ route('coupons.check') }}" method="POST">
          @csrf
           <input type="text" name="code" class="form-control text-center" placeholder=" 
               {{ __('cupon code') }}">
            <button name="confirm_code" class="btn btn-success col-md">{{ __('Confirm') 
                }}</button>
       </form>
1 Answers

When you use with, you create a session, and to access that session, you should use session helper. So change your if statement like this:

@if(session('coupon')){{session('coupon')->discount_value }} 
             
Related