Не могу загрузить картинку через ajax в Laravel.
При попытке загрузить получаю такую ошибку "Call to a member function getClientOriginalExtension() on null" тоесть картинка не была передана контроллеру.
<form class="form-horizontal" role="form" method="POST" action="/page/profile/change/image" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="hidden" id="token" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<div class="col-md-6">
<input id="upload-image" type="file" class="form-control" name="image" value="{{ $user->image }}">
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" id="edit-image" class="btn btn-primary">
submit
</button>
</div>
</div>
</form>
$(document).ready(function(){
$('#edit-image').click(function(e){
e.preventDefault();
var token = $("input[name='_token']").val();
var image = new FormData($("#upload-image")[0]);
$.ajax({
dataType: 'json',
type:'POST',
url: '/page/profile/change/image',
data:{
'image':image,
//_token:token
},
headers: {
'X-CSRF-Token': $("input[name='_token']").val()
},
contentType: false,
processData: false,
success: function(){
console.log('g')
}
})
})
})
public function updateImage(Request $request){
$avatar = $request->file('image');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->save( public_path('/img/user/avatar/' . $filename ) );
$user = Auth::user();
$user->image = $filename;
$user->save();
return response()->json([
'success' => 'Record has been updated successfully!'
]);
}