Laravel - 消息包不可见

时间:2021-07-19 08:27:37

标签: laravel

我使用的是 Laravel 7.0。

在我的 app/Exceptions/Handler.php 方法中“渲染”。我需要为“PostTooLargeException”创建条件。

请求是否来自ajax,响应不同。

问题是如果请求不是ajax。我正在尝试返回一些错误消息,但是我想在函数“with()”或“withErrors()”中返回的任何内容都不存在于刀片视图中。

出了什么问题,为什么会这样?

我的代码:

if ($exception instanceof PostTooLargeException) {

    if ($request->ajax()) {
        return response(['message' => "The file you are trying to send is too large "], 422);
    } {
        $errors = new MessageBag();
        $errors->add('error', 'test');
        return Redirect::back()->withErrors($errors);
    }
}   

刀片:

@if($errors->any())
     <div class="alert alert-danger">
          <ul class="list-unstyled">
                 @foreach ($errors->all() as $error)
                       <li>{{ $error }}</li>
                 @endforeach
          </ul>
      </div>
 @endif

1 个答案:

答案 0 :(得分:0)

没有 MessageBag 你可以通过将数组传递给 withErrors

 return redirect()->back()->withErrors(['error' => "test"]);
相关问题