【问题标题】:I'm having trouble with "AXIOS PATCH" method我在使用“AXIOS PATCH”方法时遇到问题
【发布时间】:2019-10-10 16:03:05
【问题描述】:

我正在尝试使用“axios patch”方法更新我的数据库记录。

这是我的代码:

editClient(client) {
  let data = new FormData();
  data.append("name", this.client.name);
  data.append("email", this.client.email);
  data.append("phone", this.client.phone);
  data.append("_method", "PATCH");
  axios
    .post(`/api/clients/${client.id}`, data)
    .then(res => {
      resolve(res.data.client);
    })
    .catch(err => console.log(err.response.data));
},

我试过这段代码:

axios
    .patch(`/api/clients/${client.id}`, {
      name: this.client.name,
      phone: this.client.phone,
      email: this.client.email
    })
    .then(res => {
      resolve(res.data.client);
    })
    .catch(err => console.log(err.response.data));

但它也不起作用。

我得到的错误是

POST http://localhost:8000/api/clients/27 500(内部服务器错误)

{message: "SQLSTATE[42S22]: Column not found: 1054 Unknown co…4 02:12:57"` = updated_at:"2019-05-24 02:12:57"})", exception: "Illuminate\Database\QueryException", file: "C:\xampp\htdocs\prs3\vendor\laravel\framework\src\Illuminate\Database\Connection.php", line: 664, trace: Array(60)}
exception: "Illuminate\Database\QueryException"
file: "C:\xampp\htdocs\prs3\vendor\laravel\framework\src\Illuminate\Database\Connection.php"
line: 664
message: "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name:"2011"' in 'where clause'

当我尝试这段代码时:

axios
    .patch(`/api/clients/${client.id}`, {
      data: this.client
    })
    .then(res => {
      resolve(res.data.client);
    })
    .catch(err => console.log(err.response.data));

我得到的错误是

app.js:285 PATCH http://localhost:8000/api/clients/27 422(无法处理的实体)

{message: "The given data was invalid.", errors: {…}}
errors:
email: ["The email field is required."]
name: ["The name field is required."]
phone: ["The phone field is required."]
__proto__: Object
message: "The given data was invalid."

我是 axios 和 vue 的新手。我正在尝试学习如何使用 axios 构建和 CRUD api。

我试图找到其他方法,但我找不到任何方法。

这是我的控制器:

public function update(Client $client) {
        $val = $client ? ','.$client : '';
        $client->update($this->_val($val));
        return back();
    }

    public function _val($val) {
        return request()->validate([
            'name' => ['required', 'min:2', 'unique:clients,name'.$val],
            'email' => ['required', 'email', 'unique:clients,email'.$val],
            'phone' => ['required', 'alpha_num'],
        ]);
    }

【问题讨论】:

  • 请求运行良好。只是缺少数据,所以 Laravel 发送验证错误作为响应
  • 当我使用 data: this.client 时,它会调用 Laravel 验证错误

标签: laravel vue.js axios


【解决方案1】:

您的 axios 邮政编码很好。它工作正常。您在帖子字段中收到的错误是larvel error column not found

我猜您在保存数据时输入了某个列名的拼写错误,或者您错误地输入了未在表格中的未知列。

请提供 laravel 端代码,以便我们查看您在哪里遇到错误。 顺便说一下你的问题=>你的axios.post code是正确的

【讨论】:

  • 我用我的控制器更新了我的帖子。顺便说一句,我的“axios.post”很好。
  • 我的意思是创建一个。但更新方法不起作用
  • $this->_val($val) 我猜是返回布尔值。它将返回真或假。 Unknown column 'name:"2011"' in 'where clause' 实际上就是这个错误。
  • 对不起,我把 Die and Dump 放在了我的控制器上。顺便说一句,谢谢它现在工作了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
  • 2021-08-17
  • 2019-04-01
  • 2016-11-02
  • 2016-04-05
  • 1970-01-01
  • 2021-01-11
相关资源
最近更新 更多