Laravel I';“我对”有困难;AXIOS贴片“;方法
我正在尝试使用“axios补丁”方法更新我的数据库记录 这是我的代码:Laravel I';“我对”有困难;AXIOS贴片“;方法,laravel,vue.js,axios,Laravel,Vue.js,Axios,我正在尝试使用“axios补丁”方法更新我的数据库记录 这是我的代码: 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");
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 500(内部服务器错误)
当我尝试这个代码时:
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补丁422(不可处理实体)
我是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代码
是正确的请求工作正常。它只是缺少数据,所以当我使用数据:this.client
时,Laravel发送了验证错误作为响应。它调用Laravel验证错误我用我的控制器更新了我的帖子。顺便说一下,我的“axios.post”很好。我是说创建一个。但是更新方法不起作用,我想是$this->\u val($val)
返回bolean。它将返回ethier true或false<代码>未知列“name:”2011“在where子句中”这实际上是错误。对不起,我的坏消息是我在控制器上放置了Die和Dump。顺便说一句,谢谢,现在它成功了
{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."
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'],
]);
}