Skip to content

Commit e4266f0

Browse files
committed
add genuid
1 parent 946bc11 commit e4266f0

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/Traits/GenUid.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace LaravelEasyRepository\Mutators;
4+
5+
6+
trait GenUid
7+
{
8+
/**
9+
* Boot function from Laravel.
10+
*/
11+
protected static function boot()
12+
{
13+
parent::boot();
14+
static::creating(function ($model) {
15+
if (empty($model->{$model->getKeyName()})) {
16+
$model->{$model->getKeyName()} = uid();
17+
}
18+
});
19+
}
20+
21+
/**
22+
* Get the value indicating whether the IDs are incrementing.
23+
*
24+
* @return bool
25+
*/
26+
public function getIncrementing()
27+
{
28+
return false;
29+
}
30+
31+
/**
32+
* Get the auto-incrementing key type.
33+
*
34+
* @return string
35+
*/
36+
public function getKeyType()
37+
{
38+
return 'string';
39+
}
40+
}

0 commit comments

Comments
 (0)