Laravel 4 how to display flash message in view?

后端 未结 11 1000
粉色の甜心
粉色の甜心 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:23

    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

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

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

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

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

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

    I fixed mine by changing the session driver in config/session.php from array to file !

    0 讨论(0)
提交回复
热议问题