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

Shiva's avatar
Level 5

Getting a value from a session

I've saved a session in a database and it's got more then one value in it. So what I'm trying to get the qty value from the session.

My code to save the session

public function logout(Request $request)
    {


        $cart_details = Cart::content();

        $cart_session_test = Session::put('cart_session_test', $cart_details);

        $cart_data_test = Session::get('cart_session_test');

        $cart_test = [
            'data' => json_encode($cart_data_test),
        ];

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

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

        if(isset($carts_tbl_test))
        {
            DB::table('cart_session')->where(['customer_id' => $customer_id])->update($cart_test);
        }else{
            $cart_test['customer_id'] =   $customer_id;
            DB::table('cart_session')->insert($cart_test);
        }

        Auth::logout();
        Session::flush();

        return redirect()->route('login')->with('success', 'You`ve been successfully logged out');
    }

When I hit $cart_test and I echo it out I get

Array
(
    [data] => {"18d6934483b994fb9943b43b7d7646bf":{"rowId":"18d6934483b994fb9943b43b7d7646bf","id":"8","name":"Product 8","qty":"2","price":80,"image":"[\"hot-dog.jpg\"]","options":[],"tax":16.8,"subtotal":160}}
)

so then in my login function I did this

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

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

if(isset($carts_tbl))
{
    $cart_data = json_decode($carts_tbl->data);
        Session::put('cart_session_test', $cart_data);
}

$show_session = Session::get('cart_session_test');

echo "<PRE>";
print_r($show_session['qty']);
die();

and I got this error

FatalErrorException in CustomersController.php line 79:
Cannot use object of type stdClass as array

Line 79 is

print_r($show_session['qty']);
0 likes
0 replies

Please or to participate in this conversation.