laravel - 我在使用“AXIOS PATCH”方法时遇到问题
问题描述
我正在尝试使用“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'],
]);
}
解决方案
您的 axios 邮政编码很好。它工作正常。您在 post 字段中得到的错误是larvel error column not found
.
我猜您在保存数据时在某些列名中输入了拼写错误,或者您错误地输入了未在表中的未知列。
请提供 laravel 侧代码,以便我们可以看到您在哪里遇到错误。顺便说一下你的问题=>你axios.post code
是正确的
推荐阅读
- python - 带有多个数字的pyplot figsize
- android - 为 Xamarin Android 应用程序构建通知
- javascript - JS中根据动态对象和值过滤数组
- javascript - Express.js 抛出错误;// 重新抛出非 MySQL 错误
- spring-boot - 使会话无效并从资源服务器访问数据后的 Spring Boot OAuth2 错误
- android - 如何将 ViewPager 的第一个 Fragment 替换为另一个 Fragment 并与前一个 Fragment 处于相同位置
- arrays - 如何计算这些大于数组中第 12 个元素的数字?
- python - 使用 YOLOv3/Tensorflow 检测时出现 KeyError
- laravel - 在laravel中调度每个作业后如何登录数据库?
- julia - 从 Plots.jl 中的终端运行时显示的绘图窗口