27 lines
559 B
PHP
Executable File
27 lines
559 B
PHP
Executable File
<?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;
|
|
}
|
|
}
|