WellNo
WellNo

Reputation: 659

update some values in a Laravel session

I have two projects where I need to update some values in my session. It works in my other project, so I used the same code in my new project ( same laravel version ). Now it won't work anymore and I don't know why. I don't get any error message, like if everything is working correctly, but it doesn't.

Here is some dummy data of how an item in my session looks like:

articleNr: "16xxxxxx"
pictureUrl: "http://blablablabla...."
title: 'chocolate' 
count: 7 // thats the amount - So I want 7x chocolate 

Code:

$product_cartData = $request['product_data'];  // In product data is the articlenumber and the amount I want to update ( like if I want to buy 5x this item ) 
$all_products = $request->session()->get('products');
foreach ($product_cartData as $data) {
    foreach ($all_products as $key => $sProduct) {
        if ($sProduct['articleNr'] == $data['id']) {
            $sProduct['count'] = (int)$data['quantitie'];
        }
        // return $sProduct; <--- this returns the item with the updated quantitie 
    }
}
return session('products', $all_products); // <--- but here the quantitie isn't updated anymore 

Does someone know why? Thats exactly how I did it in my other project and its working fine.

Thanks for any help!

Upvotes: 0

Views: 3374

Answers (2)

AdamJones
AdamJones

Reputation: 601

I found that on some projects the session persistence wasn't working as expected and that each time I wanted to update a session variable it was vital to also call Session::save().

\Session::put("session_variable_name","abc");
\Session::save();

Upvotes: 1

usrNotFound
usrNotFound

Reputation: 2830

No sure why you want to return session.

To store date to a session try

session(['key' => 'value']); 

// Via a request instance...
 $request->session()->put('key', 'value');

this would be a really good place to start https://laravel.com/docs/5.4/session

Upvotes: 1

Related Questions