File tree Expand file tree Collapse file tree 2 files changed +124
-0
lines changed
src/core/src/Database/Eloquent/Concerns Expand file tree Collapse file tree 2 files changed +124
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Hypervel \Database \Eloquent \Concerns ;
6+
7+ use Hyperf \Stringable \Str ;
8+
9+ trait HasUlids
10+ {
11+ /**
12+ * Add a unique identifier to the model before it is created.
13+ */
14+ public function creating (): void
15+ {
16+ foreach ($ this ->uniqueIds () as $ column ) {
17+ if (empty ($ model ->{$ column })) {
18+ $ this ->{$ column } = $ this ->newUniqueId ();
19+ }
20+ }
21+ }
22+
23+ /**
24+ * Generate a new UUID for the model.
25+ */
26+ public function newUniqueId (): string
27+ {
28+ return strtolower ((string ) Str::ulid ());
29+ }
30+
31+ /**
32+ * Get the columns that should receive a unique identifier.
33+ */
34+ public function uniqueIds (): array
35+ {
36+ return [$ this ->getKeyName ()];
37+ }
38+
39+ /**
40+ * Get the auto-incrementing key type.
41+ */
42+ public function getKeyType (): string
43+ {
44+ if (in_array ($ this ->getKeyName (), $ this ->uniqueIds ())) {
45+ return 'string ' ;
46+ }
47+
48+ return $ this ->keyType ;
49+ }
50+
51+ /**
52+ * Get the value indicating whether the IDs are incrementing.
53+ */
54+ public function getIncrementing (): bool
55+ {
56+ if (in_array ($ this ->getKeyName (), $ this ->uniqueIds ())) {
57+ return false ;
58+ }
59+
60+ return $ this ->incrementing ;
61+ }
62+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Hypervel \Database \Eloquent \Concerns ;
6+
7+ use Hyperf \Stringable \Str ;
8+
9+ trait HasUuids
10+ {
11+ /**
12+ * Add a unique identifier to the model before it is created.
13+ */
14+ public function creating (): void
15+ {
16+ foreach ($ this ->uniqueIds () as $ column ) {
17+ if (empty ($ model ->{$ column })) {
18+ $ this ->{$ column } = $ this ->newUniqueId ();
19+ }
20+ }
21+ }
22+
23+ /**
24+ * Generate a new UUID for the model.
25+ */
26+ public function newUniqueId (): string
27+ {
28+ return (string ) Str::orderedUuid ();
29+ }
30+
31+ /**
32+ * Get the columns that should receive a unique identifier.
33+ */
34+ public function uniqueIds (): array
35+ {
36+ return [$ this ->getKeyName ()];
37+ }
38+
39+ /**
40+ * Get the auto-incrementing key type.
41+ */
42+ public function getKeyType (): string
43+ {
44+ if (in_array ($ this ->getKeyName (), $ this ->uniqueIds ())) {
45+ return 'string ' ;
46+ }
47+
48+ return $ this ->keyType ;
49+ }
50+
51+ /**
52+ * Get the value indicating whether the IDs are incrementing.
53+ */
54+ public function getIncrementing (): bool
55+ {
56+ if (in_array ($ this ->getKeyName (), $ this ->uniqueIds ())) {
57+ return false ;
58+ }
59+
60+ return $ this ->incrementing ;
61+ }
62+ }
You can’t perform that action at this time.
0 commit comments