Browse Source

Better Code Gen

Dennis 3 years ago
parent
commit
42ff82c310
1 changed files with 14 additions and 11 deletions
  1. 14 11
      database/migrations/2022_06_02_081655_referral_code.php

+ 14 - 11
database/migrations/2022_06_02_081655_referral_code.php

@@ -9,6 +9,18 @@ use Illuminate\Support\Str;
 
 
 class ReferralCode extends Migration
 class ReferralCode extends Migration
 {
 {
+    
+    public function generateCode($userid){
+        
+            $random = STR::random(8);
+            if (User::where('referral_code', '=', $random)->doesntExist()) {
+                DB::table("users")
+                    ->where("id", "=", $userid)
+                    ->update(['referral_code' => $random]);
+            }else{
+                $this->generateCode($userid);
+            }
+    }
     /**
     /**
      * Run the migrations.
      * Run the migrations.
      *
      *
@@ -23,19 +35,10 @@ class ReferralCode extends Migration
         $existing_user = User::where('referral_code', '')->orWhere('referral_code', NULL)->get();
         $existing_user = User::where('referral_code', '')->orWhere('referral_code', NULL)->get();
 
 
         foreach ($existing_user as $user) {
         foreach ($existing_user as $user) {
-            $random = STR::random(8);
-            if (User::where('referral_code', '=', $random)->doesntExist()) {
-                DB::table("users")
-                    ->where("id", "=", $user->id)
-                    ->update(['referral_code' => $random]);
-            }else{
-                $random = STR::random(8);
-                DB::table("users")
-                    ->where("id", "=", $user->id)
-                    ->update(['referral_code' => $random]);
+            $this->generateCode($user->id);
             }
             }
         }
         }
-    }
+    
 
 
 
 
     /**
     /**