Skip to content

Commit

Permalink
fix custom key routing (#30)
Browse files Browse the repository at this point in the history
Fix custom key routing support
  • Loading branch information
ahmedbally authored May 26, 2021
1 parent 27afa8c commit 94c9731
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/Traits/OptimusEncodedRouteKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ public function resolveRouteBinding($value, $field = null)
$value = (int) $value;
}

if (!is_int($value)) {
return null;
if (is_int($value) && is_null($field)) {
$value = $this->getOptimus()->decode($value);
}

$id = $this->getOptimus()->decode($value);

return $this->where($this->getRouteKeyName(), '=', $id)->first();
return $this->where($field ?? $this->getRouteKeyName(), $value)->first();
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Traits/OptimusEncodedRouteKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public function testResolveModelWithEncodedKey(): void
$this->assertEquals($user->id, $resolvedUser->id);
}

public function testResolveModelWithEncodedKeyWithCustomKey(): void
{
$user = $this->createUserWithDefaultOptimusConnection();
$email = $user->email;
$resolvedUser = $user->resolveRouteBinding($email, 'email');

$this->assertEquals($user->id, $resolvedUser->id);
}

public function testEncodedKeyIsUsedForRouteModelBinding(): void
{
$user = $this->createUserWithDefaultOptimusConnection();
Expand Down

0 comments on commit 94c9731

Please sign in to comment.