【问题标题】:How to pass data from Vue to Laravel for patch request?如何将数据从 Vue 传递到 Laravel 以获取补丁请求?
【发布时间】:2019-09-30 12:35:04
【问题描述】:

我正在尝试将补丁请求从我的 Vuetify 数据表发送到 Laravel,然后发送到 mySQL DB。

这是来自我的 controller.php、我的 api.php 和实际 Vuetify 文件的各种代码片段:

api.php:

Route::patch('machines/{id}', [
    'as'   => 'machines/{id}',
    'uses' => 'MachineController@update'
]);

机器控制器.php

$machines = Machine::find($request->id)->update();

实际的 axios 补丁请求。在 .vue 文件中:

Object.assign(this.machines[this.editedIndex], this.editedItem);

axios.patch("machines/" + this.editedItem.id, {
    editedItem: this.editedItem
})

在望远镜有效载荷部分,我得到了更新的对象,但我也得到了一条消息:

"SQLSTATE[23000]: 违反完整性约束: 1048 列不能为空。

对于所有列。

我也尝试过这个语法作为补丁方法:

if (this.editedIndex > -1) {
    Object.assign(this.machines[this.editedIndex], this.editedItem);
    axios
        .patch("machines/" + this.editedItem.id)
        .then(res => {
            this.editedItem = Object.assign({}, this.editedItem);
        })
        .catch(err => {
            console.log(err);
        });
} else {
    this.machines.push(this.editedItem);
}
this.close();

我尝试像这样设置控制器:

$machines = Machine::find($request->id);
$machines->machine_number = $request->input('machine_number');
$machines->machine_name = $request->input('machine_name');
$machines->machine_company = $request->input('machine_company');
$machines->machine_division = $request->input('machine_division');
$machines->machine_center = $request->input('machine_center');
$machines->machine_speed = $request->input('machine_speed');
$machines->save();

但我仍然遇到同样的错误。 有人可以帮助我,或者至少指出我正确的方向吗? 谢谢!

【问题讨论】:

  • 您有dd($request->all()) 看看是否有任何东西通过了吗?
  • @thisiskelvin 我刚刚做了,什么也没发生。任何地方都没有显示,没有任何改变;/。不完全是:当我将dd($request->all()); 添加到控制器时,Telescope 停止注册补丁请求。
  • 您反对使用post 代替patch 吗?并不是说这是解决方案。
  • @thisiskelvin 据我了解,为了更新表格中的选定字段,您应该使用补丁请求。我可能错了。
  • 不一定,传递id 视觉上表示update。如果没有,则应假定您正在尝试store 一条新记录。

标签: laravel vue.js http-patch


【解决方案1】:

我已经解决了我的问题:我通过 axios.patch() 请求传递了空对象,因为我设置错误。我已将对象的结构更改为 key:value 对,瞧,它成功了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 2015-03-09
    • 2016-04-12
    相关资源
    最近更新 更多