where('student_id', 'like', "stu_{$timestamp}_%") ->orderByRaw('CAST(SUBSTRING_INDEX(student_id, "_", -1) AS UNSIGNED) DESC') ->first(); if ($lastStudent) { // 提取序号并递增 $lastSequence = (int) substr($lastStudent->student_id, strrpos($lastStudent->student_id, '_') + 1); return $lastSequence + 1; } return 0; } /** * 验证学生ID格式是否正确 * * @param string $studentId * @return bool */ public static function validateStudentIdFormat(string $studentId): bool { return preg_match('/^stu_\d+_\d+$/', $studentId) === 1; } }