0

I am working on an Online Store project using Laravel 5.8 and in this project I have added a checkbox for checking if user wants to pay the price by his online wallet:

<input class="form-check-input" type="checkbox" name="pay_wallet" value="1" id="defaultCheck2">
<label class="form-check-label small mr-3" for="defaultCheck2">
    Pay with Wallet
</label>

Then at the Controller, I added this for submitting a session, if the user checked the checkbox:

try {
   if ($request->pay_wallet == '1') {
      Session::put('wallet', 'payment');
      return redirect()->back();
   }
}catch (\Exception $e) {
   dd($e);
}

Then at the View, I tried this:

@php
if(session('wallet')){
    $us_id = Auth()->user()->usr_id;
    $user_wallet = App\UserWallet::where('user_id', $us_id)->get();
    foreach($user_wallet as $uw){
        echo "
        <ul>
            <li>$uw->id</li>
            <li>$uw->balance</li>
        </ul>
        ";
    }
}
@endphp

Basically what I need to do here is to check, if the session wallet was submitted, show all the available wallets based on his User Id.

But now the problem is, when I refresh the page, it still shows the wallets. Meaning that the session wallet is still alive!

How can I solve this problem?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • That's how sessions work. Have you tried unsetting/clearing the wallet session once you are done calculating/showing the wallet balance? Also, why not get the user's wallet info inside your controller and pass it to the view? – brombeer Jul 26 '21 at 08:54
  • @brombeer I just tried adding `Session::forget('wallet');` after the `foreach` but still after redirecting shows the `
  • `s
  • –  Jul 26 '21 at 09:02
  • Note that we prefer a technical style of writing here. We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened. – halfer Aug 07 '21 at 22:54