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

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

在视图中尝试访问msg

Sessions::get('msg')

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


当前回答

这是100%的解决方案

*上面提到的解决方案不适合我,但这一个适用于我在laravel 5.8:

$status = 'Successfully Done';
return back()->with(['status' => $status]);

并接受它为:

@if(session()->has('status'))
     <p class="alert alert-success">{{session('status')}}</p>
@endif

其他回答

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

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

只需设置flash消息和重定向回从你的控制器功能。

    session()->flash('msg', 'Successfully done the operation.');
    return redirect()->back();

然后你可以在视图刀片文件中得到消息。

   {!! Session::has('msg') ? Session::get("msg") : '' !!}

在Laravel 5.5中:

return back()->withErrors($arrayWithErrors);

在使用Blade的视图中:

@if($errors->has())
    <ul>
    @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
    @endforeach
    </ul>
@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

对于拉拉维尔 5.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