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

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

在视图中尝试访问msg

Sessions::get('msg')

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


当前回答

我知道这是一个老帖子,但这个答案可能会帮助到一些人。

在Laravel 8。这对我来说是有效的:你可以将错误返回到上一页或另一页。

return Redirect::back()->withErrors(['password' => ['Invalid Username or Password']]);

这也可以:

return view('auth.login')->withErrors(['username' => ['Invalid Username or Password']]);

但是,请确保您返回的页面/视图具有与withErrors方法中传递的第一个参数(在本例中是用户名或密码)对应的字段名,并且视图中的@error指令像这样引用相同的字段

@error('password') //or @error('username')
 <span class="invalid-feedback" role="alert">
   <strong>{{ $message }}</strong>
 </span>
@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");

Try

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

在视图中调用这个

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

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

拉拉维尔 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

在这里输入链接描述

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

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

应该是:

Session::get('msg')

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