每次刷新页面时结果都是重复的

问题描述

我的数据库中有两个表-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 (将#修改为@)