Passing flash messages in the session in Laravel

You know how to pass flash messages in sessions in Laravel. In this session we will see the best way to pass flash messages. First lets see the baasic ways to set a flash message. In the controller set message as shown below.

Redirect::to('users/login')->with('message','Thanks for registering!');

To redirect to another route usually we do it as given below.

Session::flash('message','This is a message!');

In the blade template we will display it by calling the session as {{ Session::get('message') }}.

If you are making use of the bootstrap alert classes you should flash two variables into the session ie. the message and the class of your alert.

For example:

Session::flash('message','This is a message!');

Session::flash('alert-class','alert-danger');

Then call them in the view like this:


@if(Session::has('message'))

<p class="alert {{ Session::get('alert-class', 'alert-info') }}">{{Session::get('message') }}</p>

@endif