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

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

在视图中尝试访问msg

Sessions::get('msg')

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


当前回答

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

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

其他回答

它适用于我和Laravel版本是^7.0

在控制器

return back()->with('success', 'Succesfully Added');

关于Blade文件

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

有关文档,请参阅Laravel文档

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

在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 

例如

希望这能帮助到一些人。欢呼。

# Laravel-9 刀片内部,该重定向返回操作启动的地方

   return redirect()->back()->with('message', "The Message");            

刀片内部的这种形式,将在上述动作后返回

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

当我试图重定向时,我收到了这条消息:

public function validateLogin(LoginRequest $request){
    //

    return redirect()->route('sesion.iniciar')
            ->withErrors($request)
            ->withInput();

当正确的方式是:

public function validateLogin(LoginRequest $request){
    //

    return redirect()->route('sesion.iniciar')
            ->withErrors($request->messages())
            ->withInput();

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

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

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

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