Resmi upload ede biliyorum ama edit ettiğim zaman resmi upload edemiyorum hata veriyor. Resim olmadığı zaman await axios.put(
/api/employees/${id},employee.value)
dediğim zaman diger şeyler upload oluyor ama await axios.put(
/api/employees/${id},formdata,config)
dediğimde hiç biri upload olmuyor ve hata veriyor.
<script>
import useEmployee from '../composables/employee'
import { onMounted } from 'vue' import { ref } from "vue";
import axios from "axios"; import { useRouter } from "vue-router";
export default { props : { id : { require : true, type : String } },
setup(props) {
const employee = ref({
'id' : '',
'img' : '',
'name' : '',
'email' : '',
'description' : '',
'position' : '',
'salary' : ''
})
const errors = ref("")
const router = useRouter()
const getEmployee = async (id) => {
let response = await axios.get(`/api/employees/${id}`)
employee.value = response.data.data;
}
onMounted(getEmployee(props.id))
const saveEmployee = async () => {
await updateEmployee(props.id)
}
const updateEmployee = async (id) => {
try {
const formdata = new FormData();
formdata.append('img',employee.value.img)
formdata.append('name',employee.value.name)
formdata.append('email',employee.value.email)
formdata.append('description',employee.value.description)
formdata.append('position',employee.value.position)
formdata.append('salary',employee.value.salary)
let config =
{ headers: {
'Content-Type': "multipart/form-data; charset=utf-8; boundary=" + Math.random().toString().substr(2),
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
} }
// await axios.put(`/api/employees/${id}`,employee.value) it updates the data
// await axios.put(`/api/employees/${id}`,formdata,config) it's not working and get error
await router.push({ name : 'employee.index' })
} catch (error) {
if(error.response.status === 422) {
errors.value = error.response.data.errors;
}
}
}
const onFileChange = async (e) => {
employee.value = e.target.files[0]
}
return {
saveEmployee,
employee,
onFileChange,
errors,
router,
updateEmployee,
}
}
}
</script>