Skip to content

Commit 7b71fb4

Browse files
committed
Changed replace UUID to NanoID for CodeSnippet
1 parent df1db8a commit 7b71fb4

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

app/Models/CodeSnippet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace App\Models;
44

55
use App\Models\Concerns\HasAuthor;
6-
use Illuminate\Database\Eloquent\Concerns\HasUuids;
6+
use App\Models\Concerns\HasNanoId;
77
use Illuminate\Database\Eloquent\Factories\HasFactory;
88
use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Database\Eloquent\Prunable;
@@ -12,7 +12,7 @@
1212

1313
class CodeSnippet extends Model
1414
{
15-
use AsSource, Chartable, HasAuthor, HasFactory, HasUuids, Prunable;
15+
use AsSource, Chartable, HasAuthor, HasFactory, HasNanoId, Prunable;
1616

1717
/**
1818
* The attributes that are mass assignable.

app/Models/Concerns/HasNanoId.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Models\Concerns;
4+
5+
use Hidehalo\Nanoid\Client;
6+
use Illuminate\Database\Eloquent\Concerns\HasUuids;
7+
8+
trait HasNanoId
9+
{
10+
use HasUuids;
11+
12+
/**
13+
* Get the length of the unique ID.
14+
*
15+
* @return int
16+
*/
17+
public function lengthUniqueId(): int
18+
{
19+
return 8;
20+
}
21+
22+
/**
23+
* Generate a new unique key for the model.
24+
*
25+
* @return string
26+
*/
27+
public function newUniqueId()
28+
{
29+
return (new Client)->generateId($this->lengthUniqueId(), Client::MODE_DYNAMIC);
30+
}
31+
32+
/**
33+
* Determine if given key is valid.
34+
*
35+
* @param mixed $value
36+
*
37+
* @return bool
38+
*/
39+
protected function isValidUniqueId($value): bool
40+
{
41+
return true;
42+
}
43+
}

0 commit comments

Comments
 (0)