5

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
Abdul Rehman Ch
  • 157
  • 1
  • 1
  • 11

8 Answers8

21

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.

Ningappa
  • 1,279
  • 4
  • 16
  • 26
7

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.

Dinesh Sharma
  • 658
  • 8
  • 13
  • Had the same issue. Anyone know where the web middleware gets defined as default? What if I have a route where I don't want the web middleware to be used? – mwallisch Jul 21 '16 at 10:16
  • Okay. Not too hard to find. With the 'new' update also came a RouteServiceProvider where this gets set. – mwallisch Jul 24 '16 at 20:11
4

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
1

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');
krishnakumar kk
  • 289
  • 2
  • 5
1

When the validation fails, no further code is executed and the previous page is loaded again. This is the reason why session message is not working. In order to check for any validation errors use the following code snippet at the top of your blade file.

@if ($errors->any())
  @foreach ($errors->all() as $error)
    <div class="alert alert-danger alert-block">
      <button type="button" class="close" data-dismiss="alert">×</button>
      <strong>{{ $error }}</strong>
    </div>
  @endforeach
@endif
Atif Bashir
  • 319
  • 4
  • 13
0

A bit late for this forum. I encounter this problem, I've been different sites searching for the right solution but none works. Here's my case, I'm using v6.0, and I put the route inside routes\api.php.

I think there is difference of putting the route to the right place or file, can't explain more.

Here's how I solved, I transfer the route from routes\api.php to routes\web.php and thats it after so many researching I now successfully display the flash message.

tempra
  • 2,455
  • 1
  • 12
  • 27
0

Try this code

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

This worked for me

ishq
  • 833
  • 1
  • 6
  • 11
0

This will work in case "Session" fails to display errors in your blade view.

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
Tumelo Mapheto
  • 495
  • 8
  • 10