php - 使用增量添加到现有总数

标签 php mysql laravel

我的网站有一个小的筹款部分,我需要不断增加每笔交易的总金额以及我获得的赞助商数量。

我将交易设置为 POST 请求。

Route::post('/fundraiser/checkout', 'FundController@fundCheckout')->name('fundraiser-checkout');

在我的 Controller 中,我正在执行以下操作来增加 sponsors_receivedfunding_received

请注意下面的$subscription->quantity = 给出的金额。条纹 45 美元 * 100 = 4500 美分。

            $funds = DB::table('funds')->where('cin', $cin)
            ->increment('funding_received', $subscription->quantity)
            ->increment('sponsors_received');

希望继续增加 funding_received 总额和 sponsors_received

这实际上确实增加了我的收到的资金,但sponsors_received失败并出现奇怪的错误。

Symfony\Component\Debug\Exception\FatalThrowableError Call to a member function increment() on integer

如果我删除 funding_received 查询,一切正常。 (我不能在查询中使用 increment 两次吗?)

            $funds = DB::table('funds')->where('cin', $cin)
            ->increment('sponsors_received');

除了使用increment之外,还有其他方法可以添加到funding_received吗?

使用 Laravel 6+

最佳答案

查询生成器中的increment方法基本上是一个处理增量过程的自定义update调用。由于它是更新,因此它返回与 update 调用相同的结果,即更新条目的数量。这就是您收到错误的原因:

Call to a member function increment() on integer

因此,您无法链接下一个 increment 语句,因为第一个语句返回一个数字(整数)而不是查询生成器实例。但是,您可以执行以下操作:

$query = DB::table('funds')->where('cin', $cin);

$query->increment('funding_received', $subscription->quantity)
$query->increment('sponsors_received');

这将根据给定条件创建一个基本查询并单独运行每个增量。

关于php - 使用增量添加到现有总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58543968/

相关文章:

php - 在同一页面 php 中重新加载数据库条目

mysql - mysql值字段中的html标签,对吗?

php - 使用php基于mysql查询自动回复电子邮件

php - Laravel 5.4 验证器看不到输入字段

php - mysql/php - 在 while 语句中合并来自 mysql 输出的多个数组

php - Laravel 分页顺序

php - 尝试使用 jQuery 从数据库获取信息

php - 创建简单的 MySQL 更新触发器时出现问题

laravel - withTrashed 在 hasManyThrough 关系的中间模型上

laravel - 用于存储 jpg 的 Docker 卷使用情况