fix(customer): use next free id instead of stale AUTO_INCREMENT after manual id edits
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Carbon\Carbon;
|
||||
use Rooyesh\Wallet\models\Wallet;
|
||||
@@ -200,6 +201,16 @@ class Customer extends Authenticatable implements JWTSubject
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
// کد کاربری = customers.id. AUTO_INCREMENT بعد از UPDATE دستی id پایین نمیآید
|
||||
// (مثلاً ۱..۲۳ اصلاح شد ولی شمارنده روی ۵۵۱ ماند). همیشه اولین id خالی.
|
||||
static::creating(function (self $customer) {
|
||||
if (! empty($customer->id)) {
|
||||
return;
|
||||
}
|
||||
$customer->id = self::nextSequentialId();
|
||||
$customer->incrementing = false;
|
||||
});
|
||||
|
||||
static::saving(function (self $customer) {
|
||||
$mobile = self::normalizeMobile($customer->mobile);
|
||||
if ($mobile === null) {
|
||||
@@ -211,6 +222,22 @@ class Customer extends Authenticatable implements JWTSubject
|
||||
});
|
||||
}
|
||||
|
||||
/** کوچکترین id آزاد، شامل ردیفهای soft-deleted تا PK تکراری نشود. */
|
||||
public static function nextSequentialId(): int
|
||||
{
|
||||
$table = (new static)->getTable();
|
||||
$row = DB::selectOne(
|
||||
"SELECT t.id + 1 AS next_id
|
||||
FROM (SELECT 0 AS id UNION ALL SELECT id FROM `{$table}`) t
|
||||
LEFT JOIN `{$table}` c ON c.id = t.id + 1
|
||||
WHERE c.id IS NULL
|
||||
ORDER BY t.id
|
||||
LIMIT 1"
|
||||
);
|
||||
|
||||
return max(1, (int) ($row->next_id ?? 1));
|
||||
}
|
||||
|
||||
public static function normalizeMobile(?string $mobile): ?string
|
||||
{
|
||||
$digits = preg_replace('/\D+/', '', trim((string) $mobile));
|
||||
|
||||
Reference in New Issue
Block a user