If you encountered the error message 422 unprocessable entity
in Laravel, here is the solution.
The 422 code shows that the validation is failing. You can check your request in the browser's dev tools to see what is being sent in the request payload and dd($request->all())
to see what is being received by the server.
Most likely this is because you are not actually uploading a file over AJAX, which does not support multipart/form-data
by default. Take a look at this reference for the FormData API which is supported by modern browsers: https://developer.mozilla.org/en-US/docs/Web/API/FormData
.
If you are sending the file via ajax, wrap it in FormData
:
var form = new FormData();
var image = $('#image')[0].files[0];
form.append('image', image);
And send via ajax:
new form = new FormData();
var image = $('#image')[0].files[0];
form.append('image', image);
$.ajax({
url: 'upload',
data: form,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success:function(response) {
alert(response);
}
});