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

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

在视图中尝试访问msg

Sessions::get('msg')

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


当前回答

在控制器

例如

return redirect('login')->with('message',$message);

刀片锉 消息将存储在会话中而不是变量中。

例如

@if(session('message'))
{{ session('message') }}
@endif

其他回答

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

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

应该是:

Session::get('msg')

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

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

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

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

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

拉拉维尔 8

Route::post('/user/profile', function () {
// Update the user's profile...

return redirect('/dashboard')->with('status', 'Profile updated!');
});

叶片的语法

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

在这里输入链接描述

    **Try This**
    
    Try This Code 
    --- Controller ---

    return redirect('list')->with('message', 'Successfully');
    return redirect('list');
   
  ----  Blade view ------
    @if(session()->has('message'))
        <div class="alert alert-success">
            {{ session()->get('message') }}
        </div>
    @endif

与 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