first commit
This commit is contained in:
46
app/Concerns/GeneratesUniqueTeamSlugs.php
Normal file
46
app/Concerns/GeneratesUniqueTeamSlugs.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Concerns;
|
||||
|
||||
use App\Models\Team;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
trait GeneratesUniqueTeamSlugs
|
||||
{
|
||||
/**
|
||||
* Generate a unique slug for the team.
|
||||
*/
|
||||
protected static function generateUniqueTeamSlug(string $name, ?int $excludeId = null): string
|
||||
{
|
||||
$defaultSlug = Str::slug($name);
|
||||
|
||||
$query = static::withTrashed()
|
||||
->where(function ($query) use ($defaultSlug) {
|
||||
$query->where('slug', $defaultSlug)
|
||||
->orWhere('slug', 'like', $defaultSlug.'-%');
|
||||
});
|
||||
|
||||
if ($excludeId) {
|
||||
$query->where('id', '!=', $excludeId);
|
||||
}
|
||||
|
||||
$existingSlugs = $query->pluck('slug');
|
||||
|
||||
$maxSuffix = $existingSlugs
|
||||
->map(function (string $slug) use ($defaultSlug): ?int {
|
||||
if ($slug === $defaultSlug) {
|
||||
return 0;
|
||||
} elseif (preg_match('/^'.preg_quote($defaultSlug, '/').'-(\d+)$/', $slug, $matches)) {
|
||||
return (int) $matches[1];
|
||||
}
|
||||
|
||||
return null;
|
||||
})
|
||||
->filter(fn (?int $suffix) => $suffix !== null)
|
||||
->max() ?? 0;
|
||||
|
||||
return $existingSlugs->isEmpty()
|
||||
? $defaultSlug
|
||||
: $defaultSlug.'-'.($maxSuffix + 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user