Laravel 4 how to display flash message in view?

后端 未结 11 999
粉色の甜心
粉色の甜心 2021-02-04 00:33

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/g         


        
相关标签:
11条回答
  • 2021-02-04 01:13

    This works for me

    @if(Session::has('success'))
        <div class="alert-box success">
            <h2>{{ Session::get('success') }}</h2>
        </div>
    @endif
    
    0 讨论(0)
  • 2021-02-04 01:13

    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

    0 讨论(0)
  • 2021-02-04 01:15

    This link describes how to do this http://vegibit.com/flash-messages-in-laravel/

    Just tried with laravel 5 - works to me.

    0 讨论(0)
  • 2021-02-04 01:16

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

    0 讨论(0)
  • 2021-02-04 01:17

    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
    
    0 讨论(0)
  • 2021-02-04 01:19

    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
    
    0 讨论(0)
提交回复
热议问题