feat: referral_code

This commit is contained in:
2026-06-06 12:21:30 +03:30
parent 20d4336877
commit 0f5f7835e7
4 changed files with 96 additions and 2 deletions
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace App\Traits;
use Illuminate\Support\Str;
trait HasReferralCode
{
public static function bootHasReferralCode(): void
{
static::creating(function ($model) {
if (!$model->referral_code) {
$model->referral_code = static::generateReferralCode();
}
});
}
public static function generateReferralCode(): string
{
do {
$code = strtoupper(Str::random(8));
} while (static::where('referral_code', $code)->exists());
return $code;
}
}