我的网站有一个小的筹款部分,我需要不断增加每笔交易的总金额以及我获得的赞助商数量。
我将交易设置为 POST 请求。
Route::post('/fundraiser/checkout', 'FundController@fundCheckout')->name('fundraiser-checkout');
在我的 Controller 中,我正在执行以下操作来增加 sponsors_received
和 funding_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/