database - Laravel BD::rollBack 不要在我的代码中工作
问题描述
我编写了下面的代码以验证余额和历史记录将保存在数据库中或不保存的交易。但是,当我运行代码并检查数据库时,我发现余额仍在被插入银行而不是被取消。可能是什么错误?
下面是我的余额模型负责操作的方法代码
public function deposit(float $value) : Array {
DB::beginTransaction();
$totalBefore = null;
// $totalBefore = $this->amount ? $this->amount : 0;
$this->amount += number_format($value, 2, ".", '');
$deposit = $this->save();
$historic = auth()->user()->historics()->create([
'type' => 'I',
'amount' => $value,
'total_before' => $totalBefore,
'total_after' => $this->amount,
'date' => date('Ymd'),
]);
if ($deposit && $historic) {
DB::commit();
return [
'success' => true,
'message' => 'Sucesso ao recarregar!'
];
}
else {
DB::rollBack();
return [
'success' => false,
'message' => 'Falha ao recarregar!'
];
}
}
解决方案
当您使用 save() 时,它不会被 db::rollBack() 回滚。
推荐阅读
- google-analytics - gtag 代码段中的 arguments 变量是什么?
- javascript - 在滚动时获取多个 div 的数据 ID
- javascript - JavaScript 只允许数字和一定的长度,没有句点
- angular - Angular HttpClient订阅问题
- node.js - Google Drive API:更新文件名失败
- python - Python 请求有效负载格式
- node.js - 是否可以选择在不使用 eval 或 safe-eval 的情况下评估 nodejs 中的值?
- php - PHP 的 in_array() 方法如何识别数组中对象的特定实例的存在?
- python - 比较字典中的值并根据值对每个值进行处理
- scala - Log4j + Scala:如何只记录我的自定义消息?