Laravel 4 how to display flash message in view?

后端 未结 11 1190
粉色の甜心
粉色の甜心 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: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().

提交回复
热议问题