JezK
Edit File: SlugService.php
<?php namespace App\Services; use Illuminate\Support\Str; use Illuminate\Database\Eloquent\Model; class SlugService { /** * Generate a unique slug for the given model. */ public function generate(string $modelClass, string $title, ?int $exceptId = null): string { $slug = Str::slug($title); if (empty($slug)) { $slug = Str::slug(Str::random(8)); } $originalSlug = $slug; $counter = 1; while ($this->slugExists($modelClass, $slug, $exceptId)) { $slug = $originalSlug . '-' . $counter; $counter++; } return $slug; } protected function slugExists(string $modelClass, string $slug, ?int $exceptId): bool { $query = $modelClass::where('slug', $slug); if ($exceptId) { $query->where('id', '!=', $exceptId); } // Include soft-deleted records for uniqueness check if (method_exists($modelClass, 'withTrashed')) { $query->withTrashed(); } return $query->exists(); } }