The query logic itself looks fine. Have you validated the content of auth()->user()->referred_by and matched it against all affiliate_id values in your database to see if it exists?
Also, you should use first() instead of get() to retrieve a single entry because after your query you're accessing the id property as you were expecting a single entry to be fetched.