Laravel错误未传递给视图

时间:2018-01-06 21:25:59

标签: laravel error-handling laravel-blade

我无法想象为什么laravel blade没有捕获我的错误验证并且没有将它传递给我的视图。 详细

  • 我的刀片模板中确实有错误代码段
  • 以下是我的验证工作正常

我缺少什么?

谢谢

这是我看到的json消息,而不是刀片模板中的消息

  

{   消息:“给定的数据无效。”,   status_code:500   }

我使用此片段让用户了解错误

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

最后这是我正确的工作验证

        $request->validate([
        'email' => 'required|email|unique:subscribers|max:255',
    ]);

编辑:

这是web.php中的溃败

Route::post('saveemail', 'SaveSubscriberEmailController@saveEmail');

这就是方法

namespace App\Http\Controllers;


use App\subscriber;

use Carbon\Carbon;

use Illuminate\Http\Request;

use Ramsey\Uuid\Uuid;


class SaveSubscriberEmailController extends Controller
{

    public function saveEmail(Request $request)
    {
        $request->validate([
            'email' => 'required|email|unique:subscribers|max:255',
        ]);



        $uuid = Uuid::uuid4();

        $subscriber = new subscriber();
        $subscriber->email = $request->email;
        $subscriber->uuid = $uuid->toString();
        $subscriber->created_at = Carbon::now();
        $subscriber->save();

        flash('Registration conformation email has been sent. Please check your mailbox. Thank you!')->success();
        return redirect()->back();
    }
}

3 个答案:

答案 0 :(得分:1)

之前我遇到过这个问题,我能够修复它的方法是使用包含中间件\Illuminate\View\Middleware\ShareErrorsFromSession::class的中间件组来包装路由。它会将会话的错误添加到视图中。

Kernel.php班级protected $middlewareGroups数组中,它看起来像:

'web' => [
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    // other middleware
],

然后你可以在哪里申报路线:

Route::group(['middleware' => ['web']], function () { 
    Route::post('saveemail', 'SaveSubscriberEmailController@saveEmail'); 
};

答案 1 :(得分:0)

enum Direction { TOP = "top", TOP_RIGHT= "top_right", RIGHT= "right", BOTTOM_RIGHT= "bottom_right", BOTTOM= "bottom", BOTTOM_LEFT= "bottom_left", LEFT= "left", TOP_LEFT= "top_left" } var values: {[id in Direction]: string; } = {}; values[Direction.TOP] = "test"; let button = document.createElement('button'); button.textContent = "Test"; button.onclick = function() { alert(values[Direction.TOP]); } document.body.appendChild(button);

将此路由放入Route::post('saveemail', 'SaveSubscriberEmailController@saveEmail');中间件。你可以这样做

web

将您的控制器更改为此。

Route::middleware(['web'])->group(function () {

   Route::post('saveemail', 'SaveSubscriberEmailController@saveEmail');

});

}

希望这有帮助

答案 2 :(得分:0)

请求验证仅发送422而不是500的错误,如果由于其他原因而导致此错误,并且formRequest错误包不会捕获此错误。