Я пытаюсь загрузить изображение с помощью ajax в Laravel, но когда я это делаю, я получаю это сообщение Call to a member function getClientOriginalExtension() on null
. Я не знаю, если что-то не так с моим кодом. Справка!
ProductController
Здесь я пытаюсь отправить изображение.
public function store(Request $request)
{
$validator = Validator::make($request->input(), array(
'name' => 'required',
'category_id' => 'required',
'description' => 'required',
'price_neto' => 'required',
'iva' => 'required',
'price_total' => 'required',
'image' => 'required|image',
));
$productImage = $request->file('image');
$productImageName = time() . $productImage->getClientOriginalExtension();
$productImage->move(public_path("img/products"), $productImageName);
if ($validator->fails()) {
return response()->json([
'error' => true,
'messages' => $validator->errors(),
], 422);
}
$products = Product::create($request->all());
return response()->json([
'error' => false,
'products' => $products,
], 200);
}
Product.js
Это мой файл Product.js. Он работает правильно, но теперь мне нужно добавить изображение к продукту.
$(document).ready(function() {
$("#btn-add").click(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: 'POST',
url: '/product',
data: {
name: $("#frmAddProduct input[name=name]").val(),
category_id: $("#frmAddProduct select[name=category_id]").val(),
description: $("#frmAddProduct input[name=description]").val(),
price_neto: $("#frmAddProduct input[name=price_neto]").val(),
iva: $("#frmAddProduct input[name=iva]").val(),
price_total: $("#frmAddProduct input[name=price_total]").val(),
image: $("#frmAddProduct input[name=image]").val(),
},
dataType: 'json',
success: function(data) {
$('#frmAddProduct').trigger("reset");
$("#frmAddProduct .close").click();
window.location.reload();
},
error: function(data) {
var errors = $.parseJSON(data.responseText);
$('#add-product-errors').html('');
$.each(errors.messages, function(key, value) {
$('#add-product-errors').append('<li>' + value + '</li>');
});
$("#add-error-bag").show();
}
});
});
});
function addProductForm() {
$(document).ready(function() {
$("#add-error-bag").hide();
$('#addProductModal').modal('show');
});
}
Product.blade.php
<div class="modal fade" id="addProductModal">
<div class="modal-dialog">
<div class="modal-content">
<form id="frmAddProduct">
<div class="modal-header">
<h5 class="modal-title">
<span class="fw-mediumbold">New</span>
<span class="fw-light"> Product</span>
</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="alert alert-danger" id="add-error-bag">
<ul id="add-product-errors"></ul>
</div>
<div class="row">
{{-- another code --}}
<div class="col-md-6">
<div class="form-group">
<label>Imagen</label>
<input class="form-control-file" id="image" name="image" required="" type="file">
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" id="btn-add" class="btn btn-primary">Add</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>