user8872543
user8872543

Reputation:

Laravel How to clear session after display in specific page

I can store input value in session and display it in blade, but when I back to form and add new item, the previous item still in the session and can't display the new one!! I tried to add forget to session but didn't work, I'm using ajax to pass value to controller

$(document).on('click', '.btn_getbids', function() {
$.ajax({
    type: 'post',
    url: 'addItem',
    data: {
        '_token': $('input[country=_token]').val(),
        'country': $('input[name=country]').val() },
    success: function(data) {
   }, });
   $('#country').val('');});

Controller

  public function addItem(Request $request) {

   $request->session()->put('country', $request->country);

    }

customer.blade

    <div class="alert alert-success">
         {{Session::get('country')}} 
    </div>

Upvotes: 3

Views: 15537

Answers (3)

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

You should try this add below code in your controller:

if($request->session()->has('country') && $request->session()->get('country') != '') {
  $request->session()->forget('country');
} else {
  $request->session()->put('country', $request->country);
}

Upvotes: 3

Anatoliy Babushka
Anatoliy Babushka

Reputation: 36

Try to use the flash method

$request->session()->flash('country', $request->country);

Upvotes: 1

Naushil Jain
Naushil Jain

Reputation: 444

if i get u correctly,

session->flush();

will flush away all the session data, instead use

session->forget('key');

with the key parameter, you are specifying which data you wish to clear...

Upvotes: 5

Related Questions