Ad

My Sessions Flash Messages Are Not Working In Laravel

- 1 answer

i am using FLASH in Laravel to display small notification here is how my function looks like:

public function movetotrash($id){

        $page = Pages::where('id', $id) -> first();
        $page -> active = 0;
        //$page -> save();
        \Session::flash('flash_message', 'Post has been successfully moved to trash');
        return redirect('pages');
 }

Here is how my View looks like:

<div class="col-md-12">

                    @if(Session::has('flash_message'))
                        <div class="alert alert-success">
                                {{Session::get('flash_message')}}
                        </div>
                    @endif
</div>

And here is how my Route looks like:

Route::get('/', function () {
    return view('welcome');
});

Route::get('dashboard', function () {
    return view('dashboard.dashboard');
});

Route::get('pages/trash', '[email protected]');

Route::get('pages/movetotrash/{id}', '[email protected]');

Route::resource('pages', 'PagesController');

Do i need to call some library in my controller for Session to work?

Thank you! (in advance)

Ad

Answer

From laravel docs : https://laravel.com/docs/master/routing

Any routes not placed within the web middleware group will not have access to sessions and CSRF protection, so make sure any routes that need these features are placed within the group. Typically, you will place most of your routes within this group:

Route::group(['middleware' => ['web']], function () {
     //all routes
});

Put your routes inside middleware and your problem will solve.

Ad
source: stackoverflow.com
Ad