diff --git a/config/sqids.php b/config/sqids.php index faca1d0..fff8f11 100644 --- a/config/sqids.php +++ b/config/sqids.php @@ -62,6 +62,8 @@ | the prefix and the casing. By default, the prefix will be generated | based on the model name. | + | Setting the prefix length to "0" will remove the prefix all together. + | | Supported Casing: "lower", "upper", "camel", "snake", "kebab", | "title", "studly" | diff --git a/src/HasSqids.php b/src/HasSqids.php index b280228..b440e2e 100644 --- a/src/HasSqids.php +++ b/src/HasSqids.php @@ -43,10 +43,11 @@ public function resolveRouteBindingQuery($query, $value, $field = null): Builder public static function keyFromSqid(string $sqid): ?int { + $prefixLength = Sqids::prefixLength(); $prefix = Str::beforeLast(subject: $sqid, search: Sqids::separator()); $expectedPrefix = Sqids::prefixForModel(model: __CLASS__); - if ($prefix !== $expectedPrefix) { + if ($prefixLength && $prefix !== $expectedPrefix) { return null; } diff --git a/src/Sqids.php b/src/Sqids.php index 90b3c66..0b621e1 100644 --- a/src/Sqids.php +++ b/src/Sqids.php @@ -16,7 +16,7 @@ public static function forModel(Model $model): string $id = $model->getKey(); $prefix = static::prefixForModel(model: $model::class); - $separator = static::separator(); + $separator = $prefix ? static::separator() : null; $sqid = static::encodeId(id: $id); return "{$prefix}{$separator}{$sqid}"; @@ -25,7 +25,11 @@ public static function forModel(Model $model): string public static function prefixForModel(string $model): ?string { $classBasename = class_basename(class: $model); - $prefixLength = Config::integer(key: 'sqids.prefix.length', default: 3); + $prefixLength = static::prefixLength(); + + if (!$prefixLength) { + return null; + } $prefix = $prefixLength < 0 ? $classBasename @@ -52,6 +56,11 @@ public static function separator(): string return Config::string(key: 'sqids.separator', default: '_'); } + public static function prefixLength(): int + { + return Config::integer(key: 'sqids.prefix.length', default: 3); + } + public static function encodeId(int $id): string { return static::encoder()->encode(numbers: [$id]);