php - Laravel 6 和 Vue.js 使用 vue-router 和 axios 更新子组件的记录

标签 php mysql laravel vue.js

我已经被困在使用 Vue 组件的更新方法上一个多星期了。更新发生在用户单击“更新”按钮并打开模式后。从模式中,子组件值正在更新,但我无法将数据发送到数据库(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();
            });
        }
    },
}

Controller :

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(...) 调用中。

axios.put(url, post).then(response => {
    this.getGroups();
});

编辑:请务必在数据到达时对其进行验证!

关于php - Laravel 6 和 Vue.js 使用 vue-router 和 axios 更新子组件的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59458145/

相关文章:

php - 如何从表中找出特定行 ID?

PHP DOM - 如何在特定位置插入元素?

php - 非静态方法不应该静态调用,假设 $this 来自不兼容的上下文 -Laravel 5.2

laravel - 使用 Laravel 分页增加行号

laravel - 如何确保Vuejs中挂载不同父组件时子组件不总是重新加载

php - 权威的 PHP url 解析器

mysql - 如何选择一个表中的记录而不是另一个具有多个 PKID 的记录?

php - 购物车总数量仅在更新后读取购物车中的最后一个数字

mysql - 与 Count 合并或与 Sum 合并 - MySQL

php - CakePHP 元素错误处理问题