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); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
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) |
||||||
0 ignored issues
–
show
The parameter
$field is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||
28 | { |
||||||
29 | $value = static::getDecodedId($value); |
||||||
30 | |||||||
31 | return $this->where($this->getRouteKeyName(), $value)->first(); |
||||||
0 ignored issues
–
show
The method
getRouteKeyName() does not exist on App\Traits\Hashidable . Did you maybe mean getRouteKey() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() It seems like
where() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
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); |
||||||
0 ignored issues
–
show
$this of type App\Traits\Hashidable is incompatible with the type Illuminate\Database\Eloquent\Model|integer expected by parameter $model of App\Traits\Hashidable::getEncodedId() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
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 |