Skip to content

Instantly share code, notes, and snippets.

@quantumwebco
Created December 14, 2024 15:50
Show Gist options
  • Save quantumwebco/ae3a25398960978e4d866c6fe714c8cb to your computer and use it in GitHub Desktop.
Save quantumwebco/ae3a25398960978e4d866c6fe714c8cb to your computer and use it in GitHub Desktop.
<?php
namespace App\Traits;
use Hashids\Hashids;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Str;
trait Hashid
{
protected static $hashLength = 10;
public function __construct(array $attributes = [])
{
$this->appends = array_merge($this->appends ?? [], ['hashid']);
$this->hidden = array_merge($this->hidden ?? [], ['id']);
parent::__construct();
}
public static function hashSalt()
{
$className = Str::of(self::class)->replace('\\', '_')->__toString();
return base64_encode(config('app.salt').'-'.$className.'-'.(new self)->getTable());
}
public static function hashid_encode(string|array $ids, ?int $length = null): string|array
{
$encodedArray = collect($ids)->map(function ($id) use ($length) {
return (new Hashids(self::hashSalt(), $length ?? self::$hashLength, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'))->encode($id);
});
ray(self::class, self::hashSalt(), $ids, $encodedArray);
return $encodedArray->count() > 1 ? $encodedArray->toArray() : $encodedArray->first();
}
public static function hashid_decode(string|array $ids, ?int $length = null): int|array
{
// ray($ids);
$decodedArray = collect($ids)->map(function ($id) use ($length) {
$hashids = new Hashids(self::hashSalt(), $length ?? self::$hashLength, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890');
return collect($hashids->decode($id))->first() ?? base64_decode($id);
});
ray($decodedArray->first());
return $decodedArray->count() > 1 ? $decodedArray->toArray() : $decodedArray->first();
}
/**
* Retrieve the model for a bound value.
*
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation $query
* @param mixed $value
* @param string|null $field
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function resolveRouteBindingQuery($query, $value, $field = null)
{
$col = $field ?? $this->getRouteKeyName();
return $query->where($col, $col === 'id' && $value ? (self::hashid_decode($value) ?: $value) : $value);
}
public function hashid(): Attribute
{
return Attribute::make(
get: fn() => $this->id ? self::hashid_encode($this->id) : $this->id,
);
}
public static function byHashId(string $id, array $with = [], int $length = 10): self
{
$model = self::findOrFail(self::hashid_decode($id, $length));
if (!empty($with)) {
$model->load(...$with);
}
return $model;
}
public function scopeHashids(Builder $query, array $hashids, ?int $length = null): Builder
{
if (empty($hashids)) {
return $query;
}
$ids = self::hashid_decode($hashids, $length ?? self::$hashLength);
return $query->whereIn($this->getTable().'.id', (array)$ids);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment