added function for trigger 2

This commit is contained in:
2026-07-06 19:21:20 +03:30
parent dcc8559945
commit fc3792902a
6 changed files with 60 additions and 13 deletions
+28 -5
View File
@@ -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)) {
+13
View File
@@ -9,6 +9,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Support\Facades\Hash;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Tymon\JWTAuth\Contracts\JWTSubject;
@@ -80,4 +81,16 @@ class User extends Authenticatable implements JWTSubject
public function setPasswordAttribute($value){
$this->attributes['password'] = Hash::make($value);
}
public static function isMobileTaken(?string $mobile, ?int $exceptId = null): bool
{
return Customer::isMobileTakenOnTable(self::class, $mobile, $exceptId);
}
public static function assertMobileUnique(?string $mobile, ?int $exceptId = null): void
{
if (self::isMobileTaken($mobile, $exceptId)) {
throw new UnprocessableEntityHttpException('این شماره موبایل قبلاً ثبت شده است');
}
}
}