gpt4 book ai didi

php - Laravel 错误变量

转载 作者:行者123 更新时间:2023-12-02 17:28:11 24 4
gpt4 key购买 nike

如果满足条件,我如何将自定义值转发给错误变量?我正在对用户创建进行验证,一切正常,除非我使用现有的电子邮件(这在数据库中是唯一的),然后使应用程序崩溃。

如果这可以在 CreateUserRequest 中完成,那就太好了,但我没有发现这样的东西,所以我在 Controller 中过滤掉它。

返回 JSON 的工作方式如下:

public function store(CreateUserRequest $request)
{
if (User::whereEmail($request['email'])->first()) {
return Response::json([
'error' => 'User with given e-mail already exists',
], 409);
else {
$user = User::create($request->all());
return redirect('user');
}
}

但在我看来我有:

@if ($errors->has('email'))
<span class="help-block" style="color: red">
<strong>
{{ $errors->first('email') }}
</strong>
</span>
@endif

所以我想知道是否有一种方法可以将消息从我的 Controller 转发到全局 $error 变量?

最佳答案

使用

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

但更好的方法是在 CreateUserRequest 验证中检查这一点。

// rules
'email' => 'required|unique:users',

关于php - Laravel 错误变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36763224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com