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

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

在视图中尝试访问msg

Sessions::get('msg')

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


当前回答

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

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

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

   {!! Session::has('msg') ? Session::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.8中,你可以做以下事情:

return redirect()->back()->withErrors(['name' => 'The name is required']);

在刀刃上:

@error('name')
<p>{{ $message }}</p>
@enderror

在叶片

 @if(Session::has('success'))

    <div class="alert alert-success" id="alert">
        <strong>Success:</strong> {{Session::get('success')}}
    </div>

@elseif(session('error'))
    <div class="alert alert-danger" id="alert">
        
        <strong>Error:</strong>{{Session::get('error')}}
    </div>
@endif

在控制器 成功的

 return redirect()->route('homee')->with('success','Successfully Log in '); 

为错误

 return back()->with('error',"You are not able to access");

Laravel 5.6。*

控制器

if(true) {
   $msg = [
        'message' => 'Some Message!',
       ];

   return redirect()->route('home')->with($msg);
} else {
  $msg = [
       'error' => 'Some error!',
  ];
  return redirect()->route('welcome')->with($msg);
}

叶片模板

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

Enyoj

Try

return Redirect::back()->withErrors(['msg' => 'The Message']);

在视图中调用这个

@if($errors->any())
<h4>{{$errors->first()}}</h4>
@endif