How can I forward the custom value to error variable if a condition is met? I am doing a validation on user creation, and everything works except when I use the existing e-mail (which is unique in DB), which then crashes the app.
It would be great if that could be done in CreateUserRequest but I found no such thing, so I am filtering it out in controller.
Returning JSON worked like so:
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');
}
}
But in my view I have:
@if ($errors->has('email'))
<span class="help-block" style="color: red">
<strong>
{{ $errors->first('email') }}
</strong>
</span>
@endif
So I was wondering if there is a way to forward a message from my controller to global $error variable?