In this episode, we're tackling how to actually set up a referral cookie when a user signs up using a referral link. First, we walk through updating our form so that when someone submits it, it posts to a new route where we'll handle all the referral logic.
We set up a new controller just for storing referrals, and in there, we'll increment the click count (for tracking how often referral links are used). We're also setting a cookie that contains the referral code and making sure it lasts for a month, so the user doesn't lose it. Laravel gives us a couple of ways to handle cookies, and we take a look at both, but we go with queueing the cookie for clarity.
After setting the cookie, we redirect the user to the registration page. Once they complete the signup and land on their dashboard, we check for the presence of the referral code in their cookie. To make this a bit tidier, we move dashboard logic into its own controller. That way, we can easily find and pass down the referral code (if it exists) to the dashboard view.
Finally, on the dashboard page, we show a message if the user was referred—just a note telling them they'll get a discount due to the referral. This sets us up for the future, because now we know on checkout whether a coupon should be created and applied for users who came through a referral link.