【发布时间】:2020-04-23 17:06:37
【问题描述】:
我创建了一个组件,它可以通过按一个按钮来添加其他字段。我不知道如何使用 axios.post 和 laravel 控制器将其提交到数据库中。过去我可以通过使用 jquery 和纯 laravel 来实现它,但是我很困惑如何在 vue 和 axios 中实现它。
Component.vue
<template>
<v-app>
<table class="table">
<thead>
<tr>
<td><strong>Title</strong></td>
<td><strong>Description</strong></td>
<td><strong>File</strong></td>
<td></td>
</tr>
</thead>
<tbody>
<tr v-for="(row, index) in rows" :key="row.id">
<td><v-text-field outlined v-model="row.title" /></td>
<td><v-text-field outlined v-model="row.description" /></td>
<td>
<label class="fileContainer">
<input type="file" @change="setFilename($event, row)" :id="index">
</label>
</td>
<td><a @click="removeElement(index);" style="cursor: pointer">X</a></td>
</tr>
</tbody>
</table>
<div>
<v-btn @click="addRow()">Add row</v-btn>
<v-btn class="success" @click="save()">Save</v-btn>
<pre>{{ rows | json}}</pre>
</div>
</v-app>
</template>
<script>
export default {
data: ()=> ({
rows: []
}),
methods: {
addRow() {
var elem = document.createElement('tr');
this.rows.push({
title: "",
description: "",
file: {
name: 'Choose File'
}
});
},
removeElement(index) {
this.rows.splice(index, 1);
},
setFilename(event, row) {
var file = event.target.files[0];
row.file = file
},
save() {
// axios.post
}
}
}
</script>
Controller.php
public function store(Request $request)
{
// store function
}
【问题讨论】: