如何解决每次刷新页面时结果都是重复的
我的数据库中有两个表-Laravel应用中的交易和收入。
交易表中有一个到期日期字段。该日期发生后,该交易的状态将更改为“已完成”。
在交易表中状态为“已完成”时,我想使用该交易中的少量信息在收益表中创建新记录。
我尝试了以下代码。它可以工作,但是每次刷新页面时,它都会在收入表中创建交易表中所有行的新记录-状态为“已完成”。
我做错了什么以及如何解决? 感谢您的贡献。
TransactionController.php文件
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use App\User;
use Transaction;
use Earning;
class TransactionController extends Controller
{
public function index() {
$trans = auth()->user()-transactions()->orderBy('id','desc')->get();
//Calling the statusUpdate() method here....it's a method below in the file.
(new self):: statusUpdate();
//Return the view...
return view('user.index',compact('trans');
}
public static function statusUpdate()
{
// Get the current date and time.
$now = Carbon::now();
//Update the transaction table status to 'completed'
auth()->user()->transactions()->where('expires_at','<',$now)->update([
'status' => 'Completed',]);
//Querying the database for completed transactions and creating a new earnings record...
$comTrans = auth()->user()->transactions()->where('status','Completed')->get();
//Looping through the returned array..
foreach($comTrans as $key => $value) {
$earning[$key] = new Earning();
$earning[$key]->amount = $value->amount;
$earning[$key]->balance = $value->balance;
$earning[$key]->user_id = $value->user_id;
//Save to database....
$earning[$key]->save();
}
}
}
在收入表和交易记录表之间,列数不相等。我仅从事务处理表中提取几列以在收入表中创建新记录。 同样,此方法有效,但是每当我刷新索引页面时,它都会在收益表中重新创建新记录。更像是复制它们。
感谢您的贡献。 提前致谢。 ..
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。