'datetime', 'created_at' => 'datetime', ]; } // ────────────────────────────────────────── // 公共查询方法 // ────────────────────────────────────────── /** * 获取拦截该名称的具体黑名单记录(如果有)。 * * @param string $username 要检测的用户名 * @return static|null */ public static function getBlockingRecord(string $username): ?self { // 1. 检查是否存在精确匹配且未过期的“临时改名保留”名称 $tempRecord = static::where('type', 'temp') ->where('username', $username) ->where('reserved_until', '>', now()) ->first(); if ($tempRecord) { return $tempRecord; } // 2. 检查永久禁用词,改为【模糊匹配】(只要新注册的名字中包含禁用词,拦截) // 比如数据库禁用了 "admin",那么 "admin123" 也会触发拦截 $permanentRecord = static::where('type', 'permanent') ->where('username', '!=', '') ->whereRaw('? LIKE CONCAT("%", username, "%")', [$username]) ->first(); return $permanentRecord; } /** * 判断给定名称是否被禁止使用。 * * @param string $username 要检测的用户名 */ public static function isBlocked(string $username): bool { return static::getBlockingRecord($username) !== null; } /** * 兼容旧调用:isReserved() 等同于 isBlocked()。 * * @param string $username 要检测的用户名 * * @deprecated 请使用 isBlocked() */ public static function isReserved(string $username): bool { return static::isBlocked($username); } // ────────────────────────────────────────── // 作用域(Scopes) // ────────────────────────────────────────── /** * 仅查询永久禁用词(管理员维护的列表)。 */ public function scopePermanent($query) { return $query->where('type', 'permanent'); } /** * 仅查询临时保留记录(改名后旧名)。 */ public function scopeTemp($query) { return $query->where('type', 'temp'); } }