format('Ym'); $basePrefix = sprintf('INV-%s-%s-', $token, $yearMonth); return DB::transaction(function () use ($basePrefix) { // Find the last generated code with lock $lastInvoice = Invoice::query() ->where('code', 'LIKE', "{$basePrefix}%") ->lockForUpdate() ->orderByDesc('id') ->first(); if (! $lastInvoice) { $nextSequence = 1; } else { // Extract sequence number from the tail end of string $parts = explode('-', $lastInvoice->code); $lastSequence = (int) end($parts); $nextSequence = $lastSequence + 1; } // Zero-pad sequence to 5 digits (e.g., 00001, 00002) $paddedSequence = str_pad((string) $nextSequence, 5, '0', STR_PAD_LEFT); return $basePrefix . $paddedSequence; }); } }