Isis
Isis

Reputation: 189

Getting a value from a session in laravel

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

ErrorException in CustomersController.php line 81: Undefined property: stdClass::$qty

Line 81 is

print_r($show_session->qty);

Upvotes: 0

Views: 6122

Answers (2)

Dimitri Mostrey
Dimitri Mostrey

Reputation: 2355

You shouldn't use json encode or decode. As I prefer using the laravel helpers, Session:: becomes session()->

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

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

if($carts_tbl->count() == 1)
{
    session()->put('cart_session_test', $carts_tbl->data);
}

$show_session = session()->get('cart_session_test');

dd($show_session['qty']);

The reason is you lose object information when you json-ize it. You can even access properties directly session()->get('cart_session_test')['qty']

In your CartSession model add this: $casts = ['data' => 'object'] or $casts = ['data' => 'array'] depending on what you want. In your case 'array' should do as 'qty' seems an array item for the 'data' field. As such you don't need to worry about type juggling. Laravel will take care of that.

Better would be to make a cart_items table and save whatever is in the data field in there in a OneToMany relationship with cart_session (should be cart_sessions but it's ok). It will safe you a lot of trouble later on and simplify building your app. Been there, done that.

Upvotes: 2

Pankit Gami
Pankit Gami

Reputation: 2553

In your logout controller you are flushing the session. So when you flush the session it remove the data from the database.

So when you access the data from login, it will give you nothing.

Change your login function like this :

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

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

if($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();
}

Upvotes: 1

Related Questions