Laravel flash or session messages not expiring [ not maintained Updated ]
Asked Answered
K

3

3

Updated after some research
After some research I conclude that, my sessions are not maintained until I save them explicitly, below code works well, but WHY???? Ref here

Session::put('lets_test', 2);
Session::save();

Old Question
I'm new to laravel 5.3, and stuck at a problem. My Laravel Session or Flash messages are not expiring they display each time page is reloaded, until I use Session::flush() Below is my controller code

<?php

namespace App\Http\Controllers;


use Session;
use Auth;
use Illuminate\Http\Request;
use App\User;
use App\Hospital;
use App\Wpr;
use Helper;

class OperatorController extends Controller
{
    public $user_detail;

    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('operator');
    }
    public function store (Request $request){ //Form is being submitted here
        //My logic here
        Session::flash('user_message', 'Thank You');
        return redirect('/operator/wpr');
    }
}

I've also used Session::set('user_message', 4); and blade view

@if(Session::has('user_message'))
    <div class="message  animated tada">
        {{ Session::get('user_message') }}
    </div>
@endif

I've tried with Session::forget('user_message') but no luck. Updating my post after some research. I've got somewhat close to my problem by reading this post on stack because this question is exactly same to my problem but unfortunately it still persists, I've changed my session storage from file to database (in case file permissions to storage directory). What might be other possibilities?

Please help, thanks in advance.

Kozak answered 17/1, 2017 at 6:57 Comment(2)
Can you show your controller code? So basically flash session will be removed once its redirected to new URL or a page. Since you are reloading the same page again, The controller again sets the session and is displayed.Internuncio
Thanks code updatedKozak
K
1

Ultimately, somehow, I managed to find solution for my problem, of not sustaining sessions. I don't think its any issue related to file permission. Now I'm saving my session explicitly and removing it explicitly. Made a Helper class added two methods

public static function SetMessage($message, $type){ 
   Session::put('user_message', $message);
   Session::put('user_message_type', $type);
   Session::save();
}
public static function ForgetMessage(){ 
   Session::forget('user_message');
   Session::forget('user_message_type');
   Session::save();
}

and within Controller class

 Helper::SetMessage('Record updated successfully', 'success');

and within blade view tempalte

@if(Session::has('user_message'))
    <div class="alert alert-{{ Session::get('user_message_type') }}">
     {{ Session::get('user_message') }}
     {{ Helper::ForgetMessage('user_message') }}
    </div>
@endif

I hope this might help someone who is facing such kind problem. But why is it so, still unknown, maybe one day I'll post the reason too. More suggestions are welcomed, if this could be done in a better way.

Kozak answered 22/2, 2017 at 6:15 Comment(0)
H
0

try this:

if(isset($_SESSION['user_message'])) {
        $message = $_SESSION['user_message'];
        unset($_SESSION['user_message']);
        return $message;
    }
Herbert answered 17/1, 2017 at 7:1 Comment(1)
This snippet might be good with vanilla, but not with laravel. I've updated my question.Kozak
S
0

In your (Laravel) blade add $request->session()->forget('key'); or Session::forget('key'); at the end of if loop this will end or delete the session of that key. This may help you for more reference about session of laravel 5.3 visit laravel 5.3

Spatterdash answered 18/1, 2017 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.