Using increment to update user

Hi,
I am trying to use increment() to update the global tally of entrants to a competition, and also assign entry numbers to each one. I have the following in a user.update:after hook:

kirby()->site()->increment('successfulEntrants');
$entryNumber = kirby()->site()->successfulEntrants()->toInt();
            
$newUser->update([
   'entryNumber' => $entryNumber
]);

So when an editor updates the user, the successfulEntrants total is increased by one, and the user is given a corresponding entryNumber. The total is working as expected, but the entryNumber is always one behind, like it is using the total before it has had the increment applied. So for example, when approving the first entry the successfulEntrants changes to 1 (which is correct), but the user is given an entryNumber of 0

Can anyone see where I am going wrong here?

You have to store the result of updating in a new variable:

$newSite = kirby()->site()->increment('successfulEntrants');
$entryNumber = $newSite->successfulEntrants()->toInt();
1 Like

Super, thanks that was just the job :+1: