【发布时间】: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 验证错误