-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathHasHashid.php
More file actions
60 lines (52 loc) · 1.12 KB
/
Copy pathHasHashid.php
File metadata and controls
60 lines (52 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace Mtvs\EloquentHashids;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use InvalidArgumentException;
use Vinkla\Hashids\Facades\Hashids;
/**
* @method Model|null findByHashid($hashid)
* @method Model findByHashidOrFail($hashid)
*/
trait HasHashid
{
public static function bootHasHashid()
{
static::addGlobalScope(new HashidScope);
}
public function hashid()
{
return $this->idToHashid($this->getKey());
}
/**
* Decode the hashid to the id
*
* @param string $hashid
* @return int|null
*/
public function hashidToId($hashid)
{
return @Hashids::connection($this->getHashidsConnection())
->decode($hashid)[0];
}
/**
* Encode an id to its equivalent hashid
*
* @param int|string $id
* @return string|null
*/
public function idToHashid($id)
{
return @Hashids::connection($this->getHashidsConnection())
->encode($id);
}
public function getHashidsConnection()
{
return config('hashids.default');
}
protected function getHashidAttribute()
{
return $this->hashid();
}
}