I have a problem with the image update feature on vue js using axios. I'm successfully creating data with image, but now I'm stack in update the image
this is the template code section
<template>
<q-page-container>
<transition
enter-active-class="animated fadeInLeft"
leave-active-class="animated fadeOutRight"
appear
:duration="700"
>
<q-page>
<q-toolbar elevated class="text-primary">
<q-btn
flat
round
dense
icon="arrow_back"
@click="$router.push('/produk')"
/>
<q-toolbar-title>
Edit produk
</q-toolbar-title>
</q-toolbar>
<form>
<div class="row justify-center">
<p style=" width:90%; margin-top:10px;">Nama barang :</p>
<q-input
name="namaproduk"
dense
outlined
v-model="dataproduk.namaproduk"
lazy-rules
:rules="[
val => (val && val.length > 0) || 'Please type something'
]"
label="Nama produk"
style="width:90%; margin-right:10px; margin-left:10px; margin-bottom: 20px; justify-content: center;"
/>
<p style=" width:90%; ">Harga barang :</p>
<q-input
name="harga"
dense
outlined
v-model="dataproduk.harga"
type="number"
label="Harga produk"
style="width:90%; margin-right:10px; margin-left:10px; margin-bottom: 20px; justify-content: center;"
lazy-rules
:rules="[
val => (val && val.length > 0) || 'Please type something'
]"
/>
<p style=" width:90%;">Deskripsi :</p>
<q-editor
v-if="dataproduk.deskripsi"
name="deskripsi"
style="width:90%; margin-right:10px; margin-left:10px; margin-bottom: 20px; justify-content: center;"
v-model="dataproduk.deskripsi"
:definitions="{
bold: { label: 'Bold', icon: null, tip: 'My bold tooltip' }
}"
/>
<p style=" width:90%;">Gambar sekarang :</p>
<img
v-if="dataproduk.gambar"
:src="'http://127.0.0.1:8000/storage/' + dataproduk.gambar"
style="width:150px; height:150px;"
/>
<p style=" width:90%;">Ubah Gambar :</p>
<q-file
name="gambar"
@change="handleFileObject()"
dense
outlined
v-model="image"
label="Gambar produk"
style="width:90%; margin-right:10px; margin-left:10px; margin-bottom: 20px; "
/>
<q-btn
@click.prevent="ubah"
type="submit"
dense
color="primary"
label="Edit"
style="width:90%; margin: 20px; justify-content: center;"
/>
</div>
</form>
</q-page>
</transition>
</q-page-container>
</template>
This method is used to modify data
methods: {
handleFileObject() {
this.image = this.$refs.file.files[0];
},
ubah() {
var dataproduk = new FormData();
dataproduk.append("namaproduk", this.dataproduk.namaproduk);
dataproduk.append("harga", this.dataproduk.harga);
dataproduk.append("deskripsi", this.dataproduk.deskripsi);
dataproduk.append("gambar", this.image);
_.each(this.dataproduk, (value, key) => {
dataproduk.append(key, value);
});
axios
.put(
"http://127.0.0.1:8000/api/produk/update/" + this.id,
this.dataproduk,
{
headers: {
"Content-Type":
"multipart/form-data; charset=utf-8; boundary=" +
Math.random()
.toString()
.substr(2)
}
}
)
.then(response => this.$router.push("/indexadmin"))
.catch(err => {
if (err.response.status === 422) {
this.errors = [];
_.each(err.response.data.errors, error => {
_.each(error, e => {
this.errors.push(e);
});
});
}
});
}
if I do not use "content-type:multipart/form-data" data in the form of strings and integers it is successfully changed. if I use "content-type:multipart/form-data" then all data fails to be update
Here's an error adding "content-type:multipart/form-data"
exception: "Illuminate\Database\QueryException" file: "D:\magang\serverapi\vendor\laravel\framework\src\Illuminate\Database\Connection.php" line: 678 message: "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'namaproduk' cannot be null (SQL: update
produkdatassetnamaproduk= ?,deskripsi= ?,harga= ?,produkdatas.updated_at= 2021-04-09 01:50:07 whereid= 13)" trace: [{file: "D:\magang\serverapi\vendor\laravel\framework\src\Illuminate\Database\Connection.php",…},…]
Integrity constraint violation: 1048 Column 'namaproduk' cannot be nullYou have a constraint in your DBMS preventing the columnnamaprodukfrom being null. Meaning that there is no data being added to that column. Add some data to that column and it will insert or change the column constraints to allow null by removingnot nullin the table definition. If that is your blob column, make sure that you are handling blobs correctly on the server-side