我试图重定向到上一页的消息时,有一个致命的错误。

App::fatal(function($exception)
{
    return Redirect::back()->with('msg', 'The Message');
}

在视图中尝试访问msg

Sessions::get('msg')

但是什么都没有渲染,是我做错了吗?


当前回答

我也遇到过同样的问题,这个方法奏效了。

控制器

return Redirect::back()->withInput()->withErrors(array('user_name' => $message));

View

<div>{{{ $errors->first('user_name') }}}</div>

其他回答

与 Laravel 5.8 相关:

控制器

return back()->with('error', 'Incorrect username or password.');

叶片

  @if (Session::has('error'))
       <div class="alert alert-warning" role="alert">
           {{Session::get('error')}}
       </div>
  @endif

在Laravel 5.4中,以下内容对我有用:

return back()->withErrors(['field_name' => ['Your custom message here.']]);

你有一个错误(拼写错误):

Sessions::get('msg')// an extra 's' on end

应该是:

Session::get('msg')

我想,现在应该有用了,对我来说确实有用。

另一种方法是

控制器

use Session;
       
Session::flash('message', "Special message goes here");
return Redirect::back();

View

@if (Session::has('message'))
   <div class="alert alert-info">{{ Session::get('message') }}</div>
@endif

Laravel 5及以后

控制器

 return redirect()->back()->with('success', 'your message,here');   

叶:

@if (\Session::has('success'))
    <div class="alert alert-success">
        <ul>
            <li>{!! \Session::get('success') !!}</li>
        </ul>
    </div>
@endif