Laravel view:
<form enctype="multipart/form-data" method="post" name="imagesform" id="imagesform" >
<input type="hidden" name="_token" value="{{ csrf_token()}}">
<div>
<label id="show" for="files" class="button">Upload photo</label>
<input id="files" name="images" style="visibility:hidden;" type="file">
</div>
<button type="submit" class="save" id="saveImage" style="border-radius: 8px; padding: 5px 15px;">SAVE</button>
</form>
This is my code for uploading images(it takes place inside my bootstrap model).When I upload the image,and click on the submit button,the image should inserted into db and the same should be retrieved and displayed on the view page.
Ajax code:
$("#saveImage").click(function(e){
// var formdata=new FormData($("#imagesform")[0]);
//alert(formdata);
$.ajaxSetup({
headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
});
$.ajax({
url: 'saveImages/{{$dataId}}',
type: 'POST',
data:new FormData($("#imagesform")),
success: function (data) {
alert(data)
},
cache: false,
processData: false
});
return false;
});
This is the ajax code,I have tried.But the alert of formdata shows [object FormData] and browser console shows Method not allowed exception
Laravel Route:
Route::post('saveImages/{dataId}',['as' => 'saveImages','uses'=>'priceDetails@saveImages']);
Controller:
public function saveImages(Request $imagesform,$dataId)
{
if (Input::hasFile('images'))
{
$name = $this->getImageId().".".$imagesform->file('images')->getClientOriginalExtension();
$image = $imagesform->file('images');
$resize = Image::make($image)->resize(700, 300)->encode($imagesform->file('images')->getClientOriginalExtension());
$hash = md5($resize->__toString());
$path = "public/" . $name;
Storage::put($path, $resize->__toString());
}
$insertImages=new photosModel;
$insertImages->imagesid=$this->getimagesId();
$insertImages->deviceCategoryId=$dataId;
$insertImages->uploadedImages=$name;
$insertImages->save();
return $this->formImages($dataId);
}
public function formImages($dataId){
$dataId=$dataId;
$images=photosModel::all();
return view('addProductDetails1')->with('images',$images)->with('dataId',$dataId);
}