Michael Adam Michael Adam - 5 months ago 14
PHP Question

What is the difference in Laravel Validation errors output?

I am trying to output errors on my form validation using this two codes:

if ($validator->fails()) {
return redirect('/')
->withInput()
->withErrors($validator);
}


and

if ($validator->fails()) {
return redirect('/')
->withErrors($validator);
}


they are doing exactly the same thing as what I'm trying to do with my application.
Can any body explain it why? if possible with reference.

Answer

Of course it will return same message. The difference only withInput().

If you put withInput() statement it will return old value from form.

For example:

<input type="text" name="username" value="{{ old('username') }}">

If text username is invalid it will return the value back to the input.

If you didn't put withInput() it won't return anything. Only the validator message.

You can get more reference at here:

Laravel Docs about old input