Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shiva's avatar
Level 5

Creating a session

I'm trying to create a session called cart_session and when I print the session I get a blank.

use Session;

class CustomersController extends Controller
{
    public function logout(Request $request)
    {
        $cart_data = Session::get('cart_session');

        echo "<PRE>";
        print_r($cart_data);
        die();
    }
}
0 likes
5 replies
Shiva's avatar
Level 5

Here is my full code

 public function postLogin(Request $request)
    {

        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required',
        ]);

        if(Customer::where('email', '=', Input::get('email'))->exists())
        {
            if(auth('customer_admin')->attempt(array('email' => $request->input('email'), 'password' => $request->input('password'))))
            {
                if(auth()->guard('customer_admin')->user()->is_activated == '0')
                {
                    $this->logout();
                    return back()->with('error', 'Please activate your account');
                }
                $name = auth()->guard('customer_admin')->user()->name;
                $customer_id = auth()->guard('customer_admin')->user()->id;

                $carts_tbl = DB::table('cart_session')->where(['customer_id' => $customer_id])->first();

                if(!empty($carts_tbl))
                {
                    $cart_data = json_decode($carts_tbl->data);
                    Session::put('cart_session', $cart_data);
                }else{
                    $cart_data = '';
                    Session::put('cart_session', $cart_data);
                }

                return redirect()->route('customer.dashboard');
            }else{
                return back()->with('error', 'Your email/password combination is wrong');
            }
        }else{
            return back()->with('error', 'Please <a href="'.route('customer.signup').'">sign up</a>');
        }
    }

    public function logout(Request $request)
    {

        $auth_id = auth()->guard('customer_admin')->user()->id;

        $cart_data = Session::get('cart_session');

        $cart = [
            'data'      => json_encode($cart_data),
        ];

        dd($cart);
    }

What I'm trying to do here is when a customer logs in the a session is created and then when they log out it gets saved into the database. At the moment I'm just trying to echo out the data to make sure I'm getting the right thing.

KenoKokoro's avatar

As I can see, the cart_session is not set when you call the logout method. You try to access it before you set it.

Shiva's avatar
Level 5

It should be set in log in. Doesn't this set it

Session::put('cart_session', $cart_data);
KenoKokoro's avatar

Yes you are right but as you can see:

               if(auth()->guard('customer_admin')->user()->is_activated == '0')
                {
                    $this->logout();
                    return back()->with('error', 'Please activate your account');
                }

Here it is where you call the logout. At this time the session is not set. Maybe what you can try is:

  1. Open tinker
  2. Session::put('car_session', 'test data');
  3. Try to login again, you should have something in this session key.

Please or to participate in this conversation.