Search code examples
phplaravellaravel-5.8laravel-session

Laravel 5.8: Request session does not seem to setting the session at Controller


My request session doesn't seem to set the session in the controller. As you can see by saying $request->session()->flash('charge_wallet',$wallet);, I tried to set the session charge_wallet.

Then in the view, bankGateWay.getToken, I tried checking if the session exists or not. But it returns not exists somehow! So what is going wrong here? How can I fix this issue?

Controller

public function chargeWallet(Request $request, $wallet, $user)
{
    try {
        $data = $request->validate([
            'charge' => 'required|integer|min:1000',
        ]);

        $payment = Payment::create([
            'pay_type_id' => '191',
            'pay_date' => jdate()->format('Y/m/d'),
            'pay_amount' => $data['charge'] * 10,
            'pay_ord_id' => '',
            'pay_status_id' => '181',
            'pay_creator_id' => auth()->user()->usr_id,
        ]);

        $request->session()->flash('charge_wallet', $wallet); // Setting Session

        $sendInformation = new BankGateWay($payment->pay_id, 
            $payment->pay_amount, $payment->created_at->timestamp,
            route('returnFromGateCharge'));
        
        return view("bankGateWay.getToken", compact('sendInformation'));
    } catch (\Exception $e) {
        return redirect()->back()->with('error', 'Wrong Data');
    }
}

View

@if(Session::has('charge_wallet'))
    @dd('exists');
@else
    @dd('not exists');
@endif

Solution

  • public function chargeWallet(Request $request, $wallet, $user)
        {
            try {
                $data = $request->validate([
                    'charge' => 'required|integer|min:1000',
                ]);
    
                $payment = Payment::create([
                    'pay_type_id' => '191',
                    'pay_date' => jdate()->format('Y/m/d'),
                    'pay_amount' => $data['charge'] * 10,
                    'pay_ord_id' => '',
                    'pay_status_id' => '181',
                    'pay_creator_id' => auth()->user()->usr_id,
                ]);
    
                $sendInformation = new BankGateWay($payment->pay_id, $payment->pay_amount, $payment->created_at->timestamp, route('returnFromGateCharge'));
                return view("bankGateWay.getToken", compact('sendInformation'))->with('charge_wallet',$wallet);
            }catch (\Exception $e) {
                return redirect()->back()->with('error', 'Wrong Data');
            }
        }