From 712101c3f455994e362037bf955103a8423165ec Mon Sep 17 00:00:00 2001 From: Julian Prieber <60265788+JulianPrieber@users.noreply.github.com> Date: Fri, 1 Dec 2023 16:19:51 +0100 Subject: [PATCH] Now generating new link IDs at random --- app/Models/Link.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/app/Models/Link.php b/app/Models/Link.php index 3cc35c6..6b39f5e 100644 --- a/app/Models/Link.php +++ b/app/Models/Link.php @@ -5,11 +5,29 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; - class Link extends Model { use HasFactory; protected $fillable = ['link', 'title', 'button_id', 'type_params', 'typename']; + protected static function boot() + { + parent::boot(); + + static::creating(function ($link) { + if (config('linkstack.disable_random_link_ids') != 'true') { + $numberOfDigits = config('linkstack.link_id_length') ?? 9; + + $minIdValue = 10**($numberOfDigits - 1); + $maxIdValue = 10**$numberOfDigits - 1; + + do { + $randomId = rand($minIdValue, $maxIdValue); + } while (Link::find($randomId)); + + $link->id = $randomId; + } + }); + } }