-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tokenable.php
51 lines (42 loc) · 1.03 KB
/
Tokenable.php
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
<?php
namespace KDuma\Eloquent;
use Config;
use Hashids\Hashids;
/**
* Class Tokenable.
*/
trait Tokenable
{
/**
* @return Hashids
*/
private function getHashingInstance()
{
$salt = Config::get('app.key').($this->salt ?: $this->getTable());
$min_hash_length = $this->length ?: 10;
$alphabet = $this->alphabet ?: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
return new Hashids($salt, $min_hash_length, $alphabet);
}
/**
* @return string
*/
public function getTokenAttribute()
{
$hashids = $this->getHashingInstance();
return $hashids->encode($this->id);
}
/**
* @param $query
* @param $token
* @return bool|int
*/
public function scopeWhereToken($query, $token)
{
$hashids = $this->getHashingInstance();
$id = $hashids->decode($token);
if (count($id) == 0) {
return $query->whereRaw('1 = 0');
}
return $query->where('id', $id[0]);
}
}