added function for trigger 2
This commit is contained in:
+28
-5
@@ -208,14 +208,26 @@ class Customer extends Authenticatable implements JWTSubject
|
||||
|
||||
public static function isMobileTaken(?string $mobile, ?int $exceptId = null): bool
|
||||
{
|
||||
$mobile = self::normalizeMobile($mobile);
|
||||
if ($mobile === null) {
|
||||
return self::isMobileTakenOnTable(self::class, $mobile, $exceptId);
|
||||
}
|
||||
|
||||
public static function isMobileTakenOnTable(string $modelClass, ?string $mobile, ?int $exceptId = null): bool
|
||||
{
|
||||
$normalized = self::normalizeMobile($mobile);
|
||||
if ($normalized === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$query = static::query()
|
||||
->where('mobile', $mobile)
|
||||
->whereNull('deleted_at');
|
||||
if ($exceptId !== null) {
|
||||
$current = $modelClass::query()->whereNull('deleted_at')->find($exceptId);
|
||||
if ($current !== null && self::normalizeMobile($current->mobile ?? '') === $normalized) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$query = $modelClass::query()
|
||||
->whereNull('deleted_at')
|
||||
->whereIn('mobile', self::mobileLookupVariants($normalized));
|
||||
|
||||
if ($exceptId !== null) {
|
||||
$query->where('id', '<>', $exceptId);
|
||||
@@ -224,6 +236,17 @@ class Customer extends Authenticatable implements JWTSubject
|
||||
return $query->exists();
|
||||
}
|
||||
|
||||
private static function mobileLookupVariants(string $normalized): array
|
||||
{
|
||||
$digits = ltrim($normalized, '0');
|
||||
|
||||
return array_values(array_unique(array_filter([
|
||||
$normalized,
|
||||
$digits,
|
||||
'0' . $digits,
|
||||
])));
|
||||
}
|
||||
|
||||
public static function assertMobileUnique(?string $mobile, ?int $exceptId = null): void
|
||||
{
|
||||
if (self::isMobileTaken($mobile, $exceptId)) {
|
||||
|
||||
Reference in New Issue
Block a user