Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel session Flash Message Not working

Session message is not working i tried this code and many fix available online Here id my store function `

public function store(Request $request)
    {
        // dd($request->all());
        $this->validate($request, [
            'name'      =>'required',
            'username'  =>'required',
            'email'     =>'required',
            'address'   =>'required',
            'likes'     =>'required',
            'gender'        =>'required'

        ]);

        $input = $request->all();

        Contacts::create($input);

        Session::put('flash_message', 'Task successfully added!');
        return redirect()->back();
    }

And Retrieving by this code

@if(Session::has('flash_message'))
        <div class="alert alert-success">
            {{ Session::get('flash_message') }}
        </div>
@endif
like image 359
Abdul Rehman Ch Avatar asked Jan 07 '16 06:01

Abdul Rehman Ch


4 Answers

I resolved issue with laravel 6.x

As soon as I moved \Illuminate\Session\Middleware\StartSession::class and \Illuminate\View\Middleware\ShareErrorsFromSession::class from web $middlewareGroups to $middleware in app\Http\Kernel.php everything started working as expected.

like image 78
Ningappa Avatar answered Sep 28 '22 05:09

Ningappa


I Resolved the Issue with laravel 5.2.

I was having route like this

Route::group(['middleware' => [ 'web','auth']], function () {
.......
}

So Removed the web middle ware

Route::group(['middleware' => ['auth']], function () {
.......
}

and its start working

Analysis: By default Laravel Add web Middleware. check by php artisan route:list it shows web, web,auth .

so by defining it again redirect two time for the web middleware.

like image 27
Dinesh Sharma Avatar answered Sep 28 '22 06:09

Dinesh Sharma


I RESOLVED the issue with laravel 5.2.

I was having all the routes inside this:

Route::group(['middleware' => 'web'], function() {

I remove it because when I used the command php artisan route:list in the Name Middleware column the "web" shows to times: web, web.

If you have AUTH replace by:

Route::group(['middleware' => 'auth'], function() {

Also I delete a duplicate route (in routes.php). Now I just have:

Route::resource('/publicaciones', 'PublicacionesController');

My controller:

return redirect()->back()->with('success', 'Saved!'); 

My view:

  @if(Session::has('success'))
          <div class="alert alert-success">
              {{ Session::get('success') }}
          </div>
  @endif
like image 21
Martin Acosta Avatar answered Sep 28 '22 06:09

Martin Acosta


have you include the following namespace

use Session;

instead of the following code

 Session::put('flash_message', 'Task successfully added!'); 

use

 Session::flash('flash_message', 'Task successfully added!');

in instead to

return redirect()->back();

try using

 return redirect()->route('your route');
like image 38
krishnakumar kk Avatar answered Sep 28 '22 06:09

krishnakumar kk