问题
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
回答1:
This works for me
@if(Session::has('success'))
<div class="alert-box success">
<h2>{{ Session::get('success') }}</h2>
</div>
@endif
回答2:
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
回答3:
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
回答4:
two methods:
Method 1 - if you're using
return Redirect::to('users/groups')->withInput()->with('success', 'Group Created Successfully.');
under your controller create(), add in
$success = Session::get('success');
return View::make('viewfile')->with('success', $success);
then on view page,
@if (isset($success))
{{$success }}
@endif
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
return Redirect::route('users/groups')->withFlashMessage('Group Created Successfully.');
then on your view page,
@if (Session::has('flash_message'))
{{ Session::get('flash_message') }}
@endif
Method 2 is much cleaner and does not require additional code under create().
回答5:
{{ Session::get('success') }}
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,
@if(Session::has('success'))
<div class="alert-box success">
<h2>{{ Session::get('success') }}</h2>
</div>
@endif
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.
回答6:
Laravel 4.2 Personally i use
Session::flash('key', 'value');
return Redirect::to('some/url');
then in the view id first check if there is a session of that key in the view
@if(Session::has('key'))
{{Session::get('key')}} //this prints out the message or your 'value' in the session::flash method
@endif
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
回答7:
i just realized in using the Redirect::to(), when you use the withInput() method, chaining a with() function to pass variables will not work. the only way is either you flash your inputs separately using Input::flash(), and use the with() to pass your variables or you pass your variable via session using Session::flash('key','val') and retrieve in the view via session::get('key').
回答8:
This link describes how to do this http://vegibit.com/flash-messages-in-laravel/
Just tried with laravel 5 - works to me.
回答9:
this should work at localhost and on host and i tried it.
<?php $success = Session::get('success'); ?>
@if($success)
<div class="alert-box success">
<h2>{{ $success }}</h2>
</div>
@endif
回答10:
Inside of the routes.php file try to create your routes within the
Route::group(['middleware' => ['web']], function () {
//routes here
}
then use
@if(Session::has('success'))
<div class="alert-box success">
<h2>{{ Session::get('success') }}</h2>
</div>
@endif
回答11:
I fixed mine by changing the session driver in config/session.php from array to file !
来源:https://stackoverflow.com/questions/17527182/laravel-4-how-to-display-flash-message-in-view