In this episode, we're dealing with a pretty common scenario: you already have a bunch of users in your database, but you've only just built out your referral code system. The thing is, you probably want your existing users to have referral codes too! So we'll go over exactly how to do that without breaking a sweat (or your server).
We start by making a new console command that will go through all your existing users and assign them a referral code if they don't already have one. If you're imagining having to update each user by hand—don't worry, we automate the whole thing.
A big part of this is making sure our script doesn't try to load thousands (or tens of thousands!) of users into memory at once, otherwise your machine will probably complain. So, we use chunking: grabbing users in batches of 100, looping through them, and giving them referral codes as needed. It's efficient and keeps everything running smoothly.
After showing you how to test this out (even generating 10,000 users just for kicks), we make sure everything works as expected and verify it all in the database. In the end, we have a handy, reusable command you can run any time you need to retroactively give referral codes to your users. Super useful if you're upgrading an app or adding new features to your user system!