1

I am using the following code to show flash message, but it is not working.

PostController

public function store(Request $request){
$this->validate($request,array(
'title'=>'required|max:255',
'slug'  =>'required|alpha_dash|min:5|max:255|unique:posts,slug',
'body'  =>'required'
 ));

    //store in the database

    $post = new Post;
    $post->title = $request->title;
    $post->slug  = $request->slug;
    $post->body = $request->body;

    $post->save();

    //This code will generate flash message about success or failure about data insert

     Session::flash('success', 'Data has been saved successfully!');

    //Redirect to another page
    return redirect()->route('posts.show', $post->id);

}

Then to display it the following code is used:

message.blade.php

@if(Session::has('success'))
    <div class="alert alert-success" role= "alert">
        <strong>Successful:</strong>
            {{ Session::get('success') }} 
    </div>
@endif
@if(count($errors) > 0)
    <div class="alert alert-danger" role="alert">
        <strong>Errors:</strong>
            <ul>
                @foreach($errors as $error)
                    <li>  {{ $error }} </li>
                @endforeach
            </ul>
    </div>
@endif

The code above is not showing any flash message. But when Session::flash('success', 'Data has been saved successfully!'); is written as: Session::put('success', 'Data has been saved successfully!');

the flash message is displayed and does not disappear.

The routes.php is :

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

Route::get('auth/login', ['as' =>'login', 'uses'=>
     'Auth\AuthController@getLogin']);
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', ['as' => 'logout', 'uses' =>
    'Auth\AuthController@getLogout']);


Route::get('auth/register','Auth\AuthController@getRegister');
Route::post('auth/register','Auth\AuthController@postRegister');


Route::get('password/reset/{token?}',
    'Auth\PasswordController@showResetForm');
Route::post('password/email',
    'Auth\PasswordController@sendResetLinkEmail');
Route::post('password/reset','Auth\PasswordController@reset');

Route::get('contact', 'PagesController@getContact');
Route::get('about', 'PagesController@getAbout');
Route::get('/', 'PagesController@getIndex');


Route::get('reader/{slug}', ['as' => 'reader.single', 'uses' =>
    'ReaderController@getSingle'])
     ->where('slug', '[\w\d\-\_]+');


Route::get('reader', ['as' => 'reader.index', 'uses' =>
    'ReaderController@getIndex' ]);

Route::resource('posts', 'PostController');
});

Help please!

  • Possible duplicate of [ErrorBag is always empty in Laravel 5.2](http://stackoverflow.com/questions/36377420/errorbag-is-always-empty-in-laravel-5-2) – patricus Sep 10 '16 at 03:16
  • If you're using Laravel >= 5.2.27, you need to remove the `web` middleware group from your `routes.php` file. They updated it so the routes file is automatically wrapped in the `web` middleware group, so if you put that in your routes file, it runs the web middleware twice, which messes with your session data. – patricus Sep 10 '16 at 03:18
  • This has been successful. Thank you a lot for that. – Bishwajit Paul Sep 10 '16 at 05:13

1 Answers1

-1

1.- Make sure if u are including the message template on your view, in this case your SHOW view 2.- Try replacing your message template for this:

@if(Session::has('success'))
    <div class="alert alert-success" role= "alert">
        <strong>Successful:</strong>
            {!! session('success') !!}
    </div>
@endif
@if(count($errors) > 0)
    <div class="alert alert-danger" role="alert">
        <strong>Errors:</strong>
            <ul>
                @foreach($errors as $error)
                    <li>  {{ $error }} </li>
                @endforeach
            </ul>
    </div>
@endif

Look, i'm using {!! session('success') !!} instead of yours

maudev
  • 974
  • 1
  • 14
  • 32