Referral.php 418 B

1234567891011121314151617181920212223
  1. <?php
  2. namespace App\Traits;
  3. use App\Models\User;
  4. use Illuminate\Support\Str;
  5. trait Referral
  6. {
  7. public function createReferralCode()
  8. {
  9. $code = Str::random(8);
  10. // check if code already exists
  11. if (User::where('referral_code', $code)->exists()) {
  12. // if exists, generate another code
  13. return $this->generateReferralCode();
  14. }
  15. return $code;
  16. }
  17. }