I'm using Laravel 5.4 and passing the form data using JQuery. Below is the JQuery code piece
$('#upload_product').on('click', function () {
var data = $("#create_post").serializeArray();
data.push({images: jQuery.makeArray(files)});
console.log(data);
jQuery.ajax({
url: '<?php echo $edit ? 'post/update' : '/sell/upload_product';?>',
data: data,
type: "POST",
cache: false,
error: function (xhr, textStatus, errorThrown) {
console.log(xhr.status);
console.log(xhr.responseText);
console.log(errorThrown);
console.log(textStatus);
},
success: function (data) {
$('#postResult').html(data);
}
});
});
Console data
Route
Route::post('/sell/upload_product', function (Request $request) {
// echo $request[9];
return view('_components.upload_product', ['title' => $request['title'], 'availability' => $request['availability'], 'category' => $request['category'],
'subcategory' => $request['subcategory'], 'currency' => $request['currency'], 'price' => $request['price'], 'product_details' => $request['product_details'],
'community' => $request['community'], 'images' => $request['images']]);
});
But $request['images']
giving empty result.
Edit 1
I'm getting the File objects from the form using JQuery
, below is the code piece
function preview_images() {
files = event.target.files;
for (var i = 0; i <= 6; i++) {
if (i >= 1) {
$('.compPhoto' + (i + 1)).html("<img src='" + URL.createObjectURL(event.target.files[i]) + "'>");
} else {
$('.compPhoto' + (i+1)).html("<img src='" + URL.createObjectURL(event.target.files[i]) + "'>");
}
/* console.log(URL.createObjectURL(event.target.files[i]));*/
}
}
Where the function preview_images()
gets triggered when there's any change in the file input.
TIA