Skip to content

Commit

Permalink
When the id attribute is null (unsaved model), the sqid attribute sho…
Browse files Browse the repository at this point in the history
…uld return null also and not throw an error.
  • Loading branch information
mrl22 committed May 31, 2024
1 parent 2e7faa3 commit 183114e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Concerns/HasSqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected function initializeHasSqids(): void
$this->append(['sqid']);
}

public function getSqidAttribute(): string
public function getSqidAttribute(): ?string
{
return Sqids::forModel(model: $this);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Sqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@

class Sqids
{
public static function forModel(Model $model): string
public static function forModel(Model $model): ?string
{
/** @var int $id */
$id = $model->getKey();

if ($id === null) {

Check failure on line 18 in src/Sqids.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Strict comparison using === between int and null will always evaluate to false.
return null;
}

$prefix = static::prefixForModel(model: $model::class);
$separator = $prefix ? Config::separator() : null;
$sqid = static::encodeId(model: $model::class, id: $id);
Expand Down

0 comments on commit 183114e

Please sign in to comment.