laravel 我在使用“AXIOS PATCH”方法时遇到问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56286074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I'm having trouble with "AXIOS PATCH" method
提问by code for money
I am trying to update my database record using "axios patch" method.
我正在尝试使用“axios patch”方法更新我的数据库记录。
This is my code:
这是我的代码:
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));
},
I tried this code:
我试过这个代码:
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));
But it also dosn't work.
但它也不起作用。
The error i get is
我得到的错误是
POST http://localhost:8000/api/clients/27500 (Internal Server Error)
POST http://localhost:8000/api/clients/27500(内部服务器错误)
{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'
And when I tried this code:
当我尝试这段代码时:
axios
.patch(`/api/clients/${client.id}`, {
data: this.client
})
.then(res => {
resolve(res.data.client);
})
.catch(err => console.log(err.response.data));
The error i get is
我得到的错误是
app.js:285 PATCH http://localhost:8000/api/clients/27422 (Unprocessable Entity)
app.js:285 PATCH http://localhost:8000/api/clients/27422(不可处理的实体)
{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."
Im new in axios and vue. Im trying to learn on how to build and CRUD api using axios.
我是 axios 和 vue 的新手。我正在尝试学习如何使用 axios 构建和 CRUD api。
I tried to find other ways and i can't find any.
我试图找到其他方法,但找不到任何方法。
This is my controller:
这是我的控制器:
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'],
]);
}
采纳答案by Karan Sadana
You axios post code is fine. it's working fine. The error you get in post field is laarvel error column not found
.
你 axios 邮政编码很好。它工作正常。您在 post 字段中得到的错误是laarvel error column not found
.
I guess you typo some column name while saving data or you enter unknown column by mistake which is not in the table.
我猜你在保存数据时输入了一些列名,或者你错误地输入了不在表中的未知列。
Please provide laravel side code , so we can see where you facing error.
By the way from you question => Your axios.post code
is correct
请提供 Laravel 侧代码,以便我们了解您遇到错误的位置。顺便说一下你的问题=>你axios.post code
是对的