Hello i am submitiing my form through ajax.
<form id="addProductForm" action="javascript:void(0)" enctype="multipart/form-data">
<input type="hidden" id="productId">
<div class="row">
<label for="caption" class="col-md-4 col-form-label">Post Image</label>
<input type="file" class="form-control-file" id="image" name="image">
</div>
<div class="row ">
<button type="submit" id="submitAddProduct" class="btn btn-primary mr-auto ml-auto" style="margin-top:15px;">Submit</button>
</div>
</form>
This is the submit script
$('#submitAddProduct').click(function(e){
e.preventDefault();
var image = $('#image').val();
$.ajax({
type:'GET',
url:'/submitAddProduct',
async:false,
data:{
'image' : image,
},
success:function(response) {
$('#productId').val(response.msg);
}
});
});
In my controller,
public function submitAddProduct(Request $request){
$data = $request->validate([
'image' => ['required','image']
]);
public function submitAddProduct(Request $request){
$data = $request->validate([
'image' => ['required','image']
]);
// dd(request('image')->store('uploads','public'));
$imagePath = request('image')->store('uploads','public');
$image = Image::make(public_path("storage/{$imagePath}"))->fit(1200, 1200);
$image->save();
$id = Products::insertGetId($data);
if($id){
$arr = array('msg' => $id);
}
return Response()->json($arr);
}
$id = Products::insertGetId($data);
if($id){
$arr = array('msg' => $id);
}
return Response()->json($arr);
}
image val is something like C://fakepath//name; I want image to be uploaded to storage/uploads in my public folder.
But i am getting errors.
422 Unprocessable Entity
Please Help. Thank You