关于数据库中优惠券使用数量和库存加减的问题

这两处代码为什么只有一个地方用到了 where('id',$this->id)

    /**
     * 新增、减少优惠券数量
     */
    public function changeUsed($increase = true)
    {
        // 传入 true 代表新增使用量。否则代表减少使用量
        if ($increase) {
            // 与检查 SKU 库存类似,这里需要检查当前用量是否已经超过用量
            return $this->where('id', $this->id)->where('used', '<', $this->total)->increment('used');
        } else {
            return $this->decrement('used');
        }
    }
    /**
     * 减库存
     */
    public function decreaseStock($amount)
    {
        if ($amount < 0) {
            throw new InternalException('减库存不可小于0');
        }

        return $this->where('id', $this->id)->where('stock', '>=', $amount)->decrement('stock', $amount);
    }

    /**
     * 加库存
     */
    public function addStock($amount)
    {
        if ($amount < 0) {
            throw new InternalException('加库存不可小于0');
        }

        $this->increment('stock', $amount);
    }
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 3
leo

完全没看明白你在问什么

3年前 评论

@leo 在对数据进行加减操作的之前不是先找到了对应的数据的吗 为什么还要加上 where('id',this->id)这个判断呢 不是很理解这个地方

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!