Сложность заключается в отправке запроса на исправление с вложением в Laravel
бэкэнд из запроса Vue
axios
. Я попытался использовать objectToFormData для отправки запроса на публикацию, и он работал просто отлично. Однако тот же objectToFormData
даже не отправляет никаких полей из формы с запросом исправления.
Шаблон
<template>
<div>
<div id="medicalForm" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="acertLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header bg-success">
<h4 class="modal-title text-white" id="acertLabel">{{ editMode ? 'Edit Medical Record' : 'New Medical Record'}} </h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<form @submit.prevent="editMode ? updateRecord(): newFile()" id="inModal" enctype="multipart/form-data" @keydown="medicalForm.onKeydown($event)">
<div class="modal-body">
<div class="form-group">
<label>Desciption <span class="text-danger">*</span></label>
<input type="text" v-model="medicalForm.description" autocomplete="off" name="description" placeholder="describe document here" class="form-control" :class="{ 'is-invalid': medicalForm.errors.has('description') }" >
<has-error :form="medicalForm" field="description"></has-error>
</div>
<div class="form-group">
<label v-if="!editMode">Choose File <span class="text-danger"> *</span></label>
<label v-if="editMode"> Replace Existing File <span class="text-danger"> </span></label>
<input type="file" @change="handleFile" class="form-control-file" :class="{ 'is-invalid': medicalForm.errors.has('file') }">
<has-error :form="medicalForm" field="file"></has-error>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light waves-effect" data-dismiss="modal">Close</button>
<button v-show = "!editMode" type="submit" class="btn btn-success waves-effect waves-light">Create</button>
<button v-show = "editMode" type="submit" class="btn btn-warning waves-effect waves-light">Update</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
</div>
</template>
Сценарий
<script>
export default {
name: 'Medical',
data(){
return {
id: this.$route.params.id,
medicals:{},
editMode: false,
medicalForm: new Form({
'record_id': '',
'user_id': this.$route.params.id,
'description': '',
'file': null,
}),
}
},
methods:{
handleFile(e){
let selectedFile = e.target.files[0];
this.medicalForm.file = selectedFile;
},
updateRecord(){
this.medicalForm.patch('/api/medicals/'+this.medicalForm.record_id, {
transformRequest: [function (data, headers) {
return objectToFormData(data)
}],
onUploadProgress: e => {
//file is uploading
}
})
.then(response => {
//updated
})
.catch(errors => {
//not updated
});
},
newFile(){
this.medicalForm.post('/api/medicals', {
transformRequest: [function (data, headers) {
return objectToFormData(data)
}],
onUploadProgress: e => {
console.log('Saving your data...')
}
})
.then(response => {
//ok
})
.catch(errors => {
//not ok
});
},
}
}
</script>
Контроллер
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Medical;
class MedicalController extends Controller
{
public function store(Request $request)
{
dd($request); works fine
}
public function update(Request $request, $id)
{
dd($request); // does not return any values
}
}