self::GENDER_UNKNOWN, ]; /** * @var array */ protected $fillable = [ 'user_id', 'inviter_id', 'nickname', 'avatar', 'gender', 'birthday', ]; /** * @var array */ protected $casts = [ 'birthday' => 'date', ]; /** * {@inheritdoc} */ protected static function booted() { parent::saving(function ($userInfo) { // 如果没有邀请码,则自动分配邀请码 if ($userInfo->code === null) { do { $userInfo->code = Str::randomAlpha(6); } while (static::where('code', $userInfo->code)->exists()); } // 邀请码统一小写 $userInfo->code = strtolower($userInfo->code); }); } }