Laravel 4 how to display flash message in view?

2019-03-10 22:10发布

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

11条回答
Juvenile、少年°
2楼-- · 2019-03-10 22:52

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楼-- · 2019-03-10 22:53
{{ 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.

查看更多
我命由我不由天
4楼-- · 2019-03-10 22:55

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
查看更多
做个烂人
5楼-- · 2019-03-10 22:57

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().

查看更多
我想做一个坏孩纸
6楼-- · 2019-03-10 22:58

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

查看更多
登录 后发表回答