Skip to content

Commit 712101c

Browse files
committed
Now generating new link IDs at random
1 parent 6f65020 commit 712101c

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

app/Models/Link.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,29 @@
55
use Illuminate\Database\Eloquent\Factories\HasFactory;
66
use Illuminate\Database\Eloquent\Model;
77

8-
98
class Link extends Model
109
{
1110
use HasFactory;
1211

1312
protected $fillable = ['link', 'title', 'button_id', 'type_params', 'typename'];
1413

14+
protected static function boot()
15+
{
16+
parent::boot();
17+
18+
static::creating(function ($link) {
19+
if (config('linkstack.disable_random_link_ids') != 'true') {
20+
$numberOfDigits = config('linkstack.link_id_length') ?? 9;
21+
22+
$minIdValue = 10**($numberOfDigits - 1);
23+
$maxIdValue = 10**$numberOfDigits - 1;
24+
25+
do {
26+
$randomId = rand($minIdValue, $maxIdValue);
27+
} while (Link::find($randomId));
28+
29+
$link->id = $randomId;
30+
}
31+
});
32+
}
1533
}

0 commit comments

Comments
 (0)