简体   繁体   中英

Update Value of Session of only a single key in laravel

I have an input like below.

array:4 [  
  "_token" => "evktHCfCNZVQMNYXzntfHZkdNLZFqvOoYgU3yPKy"
  "name" => "Name"
  "orderId" => "5cb5943a6733a1555403834"
  "amount" => null
]

which I stored to a session like this:

public function store(Request $request)
{    
    request()->session()->put('bookingInfo', $request->input());

    return redirect()->route('book.checkout');
} 

Now I want to change the amount value in the session. How I am going to achieve that?

To update the value, you need to retrieve the values from session and update it again.

To do that, you need to do

$booking_info = $request->session()->get('bookingInfo');

Then you will got an array back. Update it like a normal array

$booking_info["amount"] = 10; // anything you wanted

If you need to put it back into session again you can do this

$request->session()->put('bookingInfo', $booking_info);

EDIT

If you want the ability to update partially, you would need to store it separately like this.

$input = $request->all();
$request->session()->put('bookingInfo.name', $input['name']);
$request->session()->put('bookingInfo.orderId', $input['orderId']);
$request->session()->put('bookingInfo.amount', $input['amount']);

Laravel 5+ // Via a request instance...

$request->session()->put('amount', 'value');

// Via the global helper...

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

// For retrieve

session('amount');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM