Skip to content

Instantly share code, notes, and snippets.

@Gummibeer
Created May 11, 2021 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gummibeer/24637886ac2a778aaaee5e0197fdf790 to your computer and use it in GitHub Desktop.
Save Gummibeer/24637886ac2a778aaaee5e0197fdf790 to your computer and use it in GitHub Desktop.
<?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