title: Results are duplicated each time I refresh the page
body: <p>I have two tables in my database - Transactions and Earnings in a Laravel app.</p>
  <p>There is an expiry date field in the transactions table. When this date occurs, the status of that transaction will change to &quot;Completed&quot;.</p>
  <p>When the status is &quot; Completed&quot; in the transactions table, I want to create a new record in the earnings table using few of the info from that transaction.</p>
  <p>I tried the below code. It works but any time  I refresh the page, it creates new records of all the rows in the transactions table - having status  'completed' - in the earnings table.</p>
  <p>What am I doing wrong and how do I fix it?
  I'll appreciate your contributions.</p>
  <p>TransactionController.php file</p>
  <pre><code>&lt;?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()-&gt;user()-transactions()-&gt;orderBy('id', 'desc')-&gt;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()-&gt;user()-&gt;transactions()-&gt;where('expires_at', '&lt;', $now)-&gt;update([
       'status' =&gt; 'Completed',
   ]);
  
  //Querying the database for completed transactions and creating a new earnings record... 
  
    $comTrans = auth()-&gt;user()-&gt;transactions()-&gt;where('status', 'Completed')-&gt;get();
  
     //Looping through the returned array..
      foreach($comTrans as $key =&gt; $value) {
  
         $earning[$key] = new Earning();
          $earning[$key]-&gt;amount = $value-&gt;amount;
          $earning[$key]-&gt;balance = $value-&gt;balance;
          $earning[$key]-&gt;user_id = $value-&gt;user_id;
  
          //Save to database.... 
          $earning[$key]-&gt;save();
  
  
       }
  
    }
  
  
  }
  </code></pre>
  <p>Between, the earnings and transactions tables do not have equal number of columns. I'm only extracting few columns from the transactions table to create new records in the earnings table.
  Also, this method works but any time I refresh the index page, it recreates new records in the earnings table. More like duplicating them.</p>
  <p>I'll appreciate your contributions.
  Thanks in advance.
  ..</p>