Created
June 9, 2022 08:38
-
-
Save Gummibeer/1ebbf4f95c4a0eab18aa1927b9a83870 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Eloquent\Concerns; | |
use Illuminate\Database\Eloquent\Model as IlluminateModel; | |
use Illuminate\Support\Arr; | |
use Vinkla\Hashids\Facades\Hashids; | |
trait HasHashId | |
{ | |
public function getRouteKey(): string | |
{ | |
return $this->getHashId(); | |
} | |
public function getHashId(?string $key = null): string | |
{ | |
return Hashids::encode($this->getAttribute($key ?? $this->getRouteKeyName())); | |
} | |
/** | |
* @param mixed $value | |
* @param string|null $field | |
* | |
* @return IlluminateModel|null | |
*/ | |
public function resolveRouteBinding($value, $field = null): ?IlluminateModel | |
{ | |
if ($field === null) { | |
$value = Arr::first(Hashids::decode($value)); | |
} | |
return parent::resolveRouteBinding($value, $field); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment