douji6199 2017-12-05 11:22 采纳率: 100%
浏览 1153
已采纳

Eloquent updateOrCreate不更新的问题

我在使用 updateOrCreate 时遇到了麻烦。

下面的代码正在成功地创建一个新行,但是在更新现有行时,它根本不起作用!

comm_helpful::updateOrCreate(
           ['ID' => 1],
           ['time' => 3000]
        );

这正如我们所预料的那样:

comm_helpful::where('ID', 1)->update(['time' => 3000]);

我已经将上面的示例剥离到最低限度(这是调试的一部分) ,但它仍然不能工作!

  • 写回答

2条回答 默认 最新

  • doushiposong30622 2017-12-05 12:58
    关注

    Ok, after almost completely pulling my hair out; I found the problem where Eloquent is setting:

    protected $primaryKey = 'id';
    

    which is used on the update method and returned by getKeyName().

    protected function setKeysForSaveQuery(Builder $query)
    {
    
        $query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery());
    
        return $query;
    }
    

    I then realised my 'id' on the tale was uppercase and not lower case!

    I guess that means my answer is that I need to ensure I am using lowercase 'id' for the primary key or overriding it on the Model

    protected $primaryKey = 'ID';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用