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
if you are using bootstrap-3 try the script below for Alert Style
This just echos the session variable 'success'. So when you use
{{ Session::get('success') }} @if($success) <div class="alert-box success"> <h2>{{ $success }}</h2> </div> @endif
you are seeing it's output along with the error of the next statement. Because with() function only sets the value in Session and will not set as a variable. Hence
@if($success)
will result in undefined variable error.As @Andreyco said,
This should work.
The reason you are not seeing it might be because the action you are performing is not success. And this does not require you to either reinstall xampp or modify php.ini.
Inside of the routes.php file try to create your routes within the
then use
two methods:
Method 1 - if you're using
under your controller create(), add in
then on view page,
What's happening in method 1 is that you're creating a variable $success that's passed into your create(), but it has no way of display $success. isset will always fail unless you set a variable to get the message and return it.
Method 2 - use return Redirect withFlashMessage
then on your view page,
Method 2 is much cleaner and does not require additional code under create().
Laravel 4.2 Personally i use
then in the view id first check if there is a session of that key in the view
it works for me most of the time and i usually have that blade template integrated into my view just so i can push success messages to the view from my codes.
please do note that it is stated in the documentation that "Sometimes you may wish to store items in the session only for the next request. You may do so using the Session::flash method" so yes it expires after the next page.
hope this helps