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

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

在视图中尝试访问msg

Sessions::get('msg')

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


当前回答

在laravel 5.8中,你可以做以下事情:

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

在刀刃上:

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

其他回答

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

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

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

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

对于拉拉维尔3

只是提醒一下@giannis christofakis的回答;对于任何使用Laravel 3替代

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

:

return Redirect::back()->with_errors(['msg', 'The Message']);

在Laravel 5.5中:

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

在使用Blade的视图中:

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

与 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