Skip to content

Commit

Permalink
feat: set custom prefix for model
Browse files Browse the repository at this point in the history
  • Loading branch information
bensherred committed Nov 27, 2023
1 parent 9542f5e commit b5f7abd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/HasSqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public function getSqidAttribute(): ?string
return Sqids::forModel(model: $this);
}

public function getSqidPrefix(): ?string
{
return $this->sqidPrefix ?? null;
}

/**
* Get the route key for the model.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Sqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public static function prefixForModel(string $model): ?string
return null;
}

/** @var string|null $modelPrefix */
/** @phpstan-ignore-next-line */
$modelPrefix = (new $model())->getSqidPrefix();

if ($modelPrefix) {
return $modelPrefix;
}

$prefix = $prefixLength < 0
? $classBasename
: rtrim(mb_strimwidth(string: $classBasename, start: 0, width: $prefixLength, encoding: 'UTF-8'));
Expand Down

0 comments on commit b5f7abd

Please sign in to comment.