我在使用Vue组件的更新方法上已经被困住了一个多星期。更新发生在用户单击" update“按钮,打开一个模式之后。从模型中,子组件值正在更新,但我无法获得要发送到DB (MySQL)的数据。任何帮助都将不胜感激。
构成部分:
<tbody>
<tr v-for="post in posts" :key="post.id">
<th>{{ post.name }}\</th>
<th>{{ post.email }}</th>
<th>{{ post.notes }}</th>
<th>{{ post.id }}</th>
<th>
<button class="" v-on:click="deleteGroup(post)">
<eva-icon name="trash-2" animation="pulse" fill="gray"></eva-icon>
</button>
<button type="button" class="btn btn-primary" data-toggle="modal" :data-target="'#exampleModal' + post.id">
<eva-icon name="edit-2" animation="pulse" fill="gray"></eva-icon>
</button>
<!-- Modal -->
<div class="modal fade" :id="'exampleModal' + post.id" tabindex="-1" role="dialog"
aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form @submit.prevent="editGroup(post)">
<div class="container shadow p-4">
<input type="hidden" v-model="post.id"/>
<div class="form-group">
<label for="group-name">Group Name</label>
<input type="text" name="name" class="form-control" v-model="post.name" placeholder="Enter the Group Name"/>
</div>
<div class="form-group">
<label for="email">Send invite Email</label>
<input type="email" name="email" class="form-control" v-model="post.email" placeholder="Enter the email"/>
</div>
<div class="form-group">
<label for="group-image">Group Image</label>
<input type="file" class="form-control" name="image">
</div>
<div class="form-group">
<label for="group-notes">Notes</label>
<textarea class="form-control" name="notes" v-model="post.notes" rows="7"></textarea>
</div>
<div class="form-group">
<button type="button" class="btn btn-success" data-dismiss="modal" @click="update(post)">
Save
</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
</div>
</div>
</th>
</tr>
</tbody>剧本:
export default {
mounted() {
console.log('Component mounted.')
},
data() {
return {
posts: [],
}
},
created() {
this.getGroups();
},
methods: {
getGroups() {
axios.get('/groups')
.then(response => this.posts = response.data);
},
deleteGroup: function (post) {
var url = 'groups/' + post.id;
axios.delete(url).then(response => {
this.getGroups();
});
},
editGroup: function (post) {
var url = 'groups/' + post.id;
axios.get(url).then(response => {
this.getGroups();
});
},
update: function (post) {
var url = 'groups/' + post.id;
axios.put(url).then(response => {
this.getGroups();
});
}
},
}主计长:
public function update(Request $request, $id)
{
$post = Group::findOrFail($id);
$post->name = $request->name;
$post->email = $request->email;
$post->notes = $request->notes;
$post->save();
return $post;
}更新:如果有任何混淆,我击中正确的路由和函数,但不能传递更新的信息,导致SQL错误,字段不能为空。
谢谢你的帮助。
发布于 2019-12-23 22:26:36
据我所见,你没有向你的路线提交任何数据。
update: function (post) {
var url = 'groups/' + post.id;
axios.put(url).then(response => {
this.getGroups();
});
}您没有将post变量的值提供给axios调用。您需要将要发送的对象作为第二个参数添加到put(...)调用中。
axios.put(url, post).then(response => {
this.getGroups();
});编辑:确保您在数据到达时验证它!
https://stackoverflow.com/questions/59458145
复制相似问题