【问题标题】:Axios.patch Not working as PATCH but as POST method [ laravel, vue ]Axios.patch 不作为 PATCH 工作,而是作为 POST 方法 [ laravel, vue ]
【发布时间】:2019-12-05 01:55:54
【问题描述】:

我正在使用 Vue.js 和引导模式创建一个小型 CRUD,但我遇到了更新用户的问题。

我使用 axios 处理任务,一旦我尝试 tupdate 用户,它就不能作为更新用户,而是作为创建用户。

我的模板文件

 <button class="btn btn-primary" @click="modal">
    <span>Create User</span>
 </button>
 <!-- Modal -->
        <div class="modal fade" id="modal" tabindex="-1" role="dialog"  aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">

                    <!-- Modal Body -->
                    <div class="modal-body">


                        <form>
                            <div class="form-group">
                                <label for="name">Name</label>
                                <input type="text" name="name" class="form-control" v-model="input.name" v-validate.continues="'required'">

                            </div> 
                            <div class="form-group">
                                <label for="name">Email</label>
                                <input type="email" name="email" class="form-control" v-model="input.email" v-validate.continues="'required'">

                            </div> 
                            <div class="form-group">
                                <label for="name">Phone Number</label>
                                <input type="text" name="phone_number" class="form-control" v-model="input.phone_number" v-validate.continues="'required'">
                            </div>
                        </form>

                    </div>
                    <div class="modal-footer">
                        <button  type="button" class="btn btn-secondary" data-dismiss="modal">
                            Close
                        </button>
                        <button v-if="add" type="button" class="btn btn-primary"  @click.prevent="createUser">
                            Create
                        </button>
                        <button v-if="edit" type="button" class="btn btn-primary" @click.prevent="updateUser">
                            Update
                        </button>
                    </div>
                </div>
            </div>
        </div>

我的脚本文件

   update(id) {

        this.add = false;
        this.edit = true;
        this.input = this.users[id];
        $('#modal').modal('show');
    },

    updateUser() {
        let $this = this;
        axios.patch('/api/users/' + this.input.id, {
            name: this.input.name,
            email: this.input.email,
            phone_number: this.input.phone_number,
        }).then(function (response) {
            console.log(response);
            $this.fetchUser();
            Swal.fire({
                position: 'top-end',
                type: 'success',
                title: 'User has been updated',
                showConfirmButton: false,
                timer: 1500
            })
            $('#modal').modal('hide');
        })
        this.$validator.validateAll();  
    },

感谢您的所有帮助。

提前谢谢..

【问题讨论】:

  • 有问题吗?
  • 它不能用作更新,而是用作创建
  • 你试过put而不是patch吗?
  • 试过了,还是一样

标签: php laravel vue.js axios


【解决方案1】:
public function update(Request $request, $id)
{
    $request->validate(['name' => 'required', 'email' => 'required|email', 'phone_number' => 'required',]);
    $user = UserModel::find($id);
    $user->update($request->input());
    return new UsersCollection($user);
}

【讨论】:

  • 它不是错误兄弟..它想更新用户并且它不更新,它创建用户
  • 你的更新方法有错误应该是这样的
  • 当我改变 $user = UserModel::find($id);它给了我两个错误 1 ​​.GET 500 (Internal server error) 2. Uncaught (in promise) SyntaxError: Unexpected token
  • 您有办公桌或团队查看器吗?
  • 不,我用的是Skype
猜你喜欢
  • 1970-01-01
  • 2020-08-01
  • 1970-01-01
  • 2016-11-04
  • 2018-12-24
  • 1970-01-01
  • 2016-09-04
  • 2021-06-30
  • 2018-09-25
相关资源
最近更新 更多