Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 how to display flash message in view?

I'm trying to get my flash message to display.

This is in my routing file

Route::post('users/groups/save', function(){

return Redirect::to('users/groups')->withInput()->with('success', 'Group Created Successfully.');

});

This is in my view

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

But nothing is working.

When I try this, I get an error Variable $success is undefined. But it actually shows the flash message too.

{{ Session::get('success') }}
@if($success)
    <div class="alert-box success">
        <h2>{{ $success }}</h2>
    </div>
@endif
like image 963
Morne Zeelie Avatar asked Jul 08 '13 12:07

Morne Zeelie


People also ask

What is session flash message?

Session:Flash Tag Flash data is session data that is only kept for a single request. It is most often used for success/failure messages that automatically disappear after a page refresh.

What is flash data in laravel?

Summary. Flashing data to the session stores short-lived data and can display it in the application. It is usually used to display status after an action. RELATED TAGS. laravel.


3 Answers

This works for me

@if(Session::has('success'))
    <div class="alert-box success">
        <h2>{{ Session::get('success') }}</h2>
    </div>
@endif
like image 123
Andreyco Avatar answered Oct 17 '22 05:10

Andreyco


if you are using bootstrap-3 try the script below for Alert Style

    @if(Session::has('success'))
        <div class="alert alert-success">
            <h2>{{ Session::get('success') }}</h2>
        </div>
    @endif
like image 6
Muthamizhchelvan. V Avatar answered Oct 17 '22 07:10

Muthamizhchelvan. V


when you set variable or message using ->with() it doesn't set the variable/message in the session flash, rather it creates an variable which is available in your view, so in your case just use $success instead of Session::get('success')

And in case you want to set the message in the session flash the use this Session::flash('key', 'value'); but remember with session flash the data is available only for next request.

Otherwise you can use Session::put('key', 'value'); to store in session

for more info read here

like image 4
Trying Tobemyself Avatar answered Oct 17 '22 07:10

Trying Tobemyself