|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Vinkla\Hashids\Facades\Hashids; |
|
7
|
|
|
|
|
8
|
|
|
trait Hashidable |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Retrieve model hashed_id attribute. |
|
12
|
|
|
* |
|
13
|
|
|
* @return string |
|
14
|
|
|
*/ |
|
15
|
|
|
public function getHashedIdAttribute() |
|
16
|
|
|
{ |
|
17
|
|
|
return static::getEncodedId($this); |
|
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Retrieve the model for a bound value. |
|
22
|
|
|
* |
|
23
|
|
|
* @param mixed $value |
|
24
|
|
|
* @param string|null $field |
|
25
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
|
26
|
|
|
*/ |
|
27
|
|
|
public function resolveRouteBinding($value, $field = null) |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
$value = static::getDecodedId($value); |
|
30
|
|
|
|
|
31
|
|
|
return $this->where($this->getRouteKeyName(), $value)->first(); |
|
|
|
|
|
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function getHashidConnection() |
|
38
|
|
|
{ |
|
39
|
|
|
return get_called_class(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $model |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function getEncodedId($model) |
|
47
|
|
|
{ |
|
48
|
|
|
$id = $model instanceof Model ? $model->id : $model; |
|
49
|
|
|
|
|
50
|
|
|
return Hashids::connection(static::getHashidConnection())->encode($id); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $hashedId |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function getDecodedId($hashedId) |
|
58
|
|
|
{ |
|
59
|
|
|
return Hashids::connection(static::getHashidConnection())->decode($hashedId)[0] ?? null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get the value of the model's route key. |
|
64
|
|
|
* |
|
65
|
|
|
* @return mixed |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getRouteKey() |
|
68
|
|
|
{ |
|
69
|
|
|
return static::getEncodedId($this); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Scope a query to get with hased ID. |
|
74
|
|
|
* |
|
75
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
76
|
|
|
* @param string $hashedId |
|
77
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
78
|
|
|
*/ |
|
79
|
|
|
public function scopeFindByHashedId($query, string $hashedId) |
|
80
|
|
|
{ |
|
81
|
|
|
$value = static::getDecodedId($hashedId); |
|
82
|
|
|
|
|
83
|
|
|
return $query->where($this->getRouteKeyName(), $value); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Scope a query to get with hased ID. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $hashedId |
|
90
|
|
|
* @param array $columns |
|
91
|
|
|
* @return \Illuminate\Database\Eloquent\Model $model |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function findByHashedIdOrFail(string $hashedId, $columns = ['*']) |
|
94
|
|
|
{ |
|
95
|
|
|
$value = static::getDecodedId($hashedId); |
|
96
|
|
|
|
|
97
|
|
|
return static::where((new static )->getRouteKeyName(), $value)->select($columns)->firstOrFail(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|