feat: referral_code
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user