Created
May 11, 2021 08:51
-
-
Save Gummibeer/24637886ac2a778aaaee5e0197fdf790 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