Php Laravel 6和Vue.js使用Vue路由器和axios从子组件更新记录
一个多星期以来,我一直坚持使用Vue组件进行更新。更新发生在用户单击“更新”按钮打开模式窗口后。从模式中,子组件值正在更新,但我无法获取要发送到DB MySQL的数据。任何帮助都将不胜感激 组成部分:Php Laravel 6和Vue.js使用Vue路由器和axios从子组件更新记录,php,mysql,laravel,vue.js,Php,Mysql,Laravel,Vue.js,一个多星期以来,我一直坚持使用Vue组件进行更新。更新发生在用户单击“更新”按钮打开模式窗口后。从模式中,子组件值正在更新,但我无法获取要发送到DB MySQL的数据。任何帮助都将不胜感激 组成部分: <tbody> <tr v-for="post in posts" :key="post.id"> <th>{{ post.name }}\</th> <th>{{ post.email }}</th>
<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>
控制器:
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错误,字段不能为空
谢谢您的帮助。据我所知,您没有向路线提交任何数据
update: function (post) {
var url = 'groups/' + post.id;
axios.put(url).then(response => {
this.getGroups();
});
}
您没有向axios调用提供post变量的值。您需要将作为第二个参数发送的对象添加到put。。。打电话
编辑:确保在数据到达时验证数据 控制台中是否有任何错误?您是否检查了“网络”选项卡以确保它指向正确的路由?PUT 500 Internal Server Error是我在consoleRoute::resource'/groups',GroupController'中得到的错误;是我的路线。看起来您正在通过提交并同时保存。我相信您应该通过表单或按钮提交,但不能同时通过表单和按钮提交。但是,我只能在前端更新组件。我无法获取要发送到控制器的更新。一直说数据字段是空的。。。。。
update: function (post) {
var url = 'groups/' + post.id;
axios.put(url).then(response => {
this.getGroups();
});
}
axios.put(url, post).then(response => {
this.getGroups();
});