Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to customize slug uniqueness resolution #611

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add functionality to call callable when provided
viicslen committed Apr 16, 2024
commit d6269030c81c42d219497cfb0b510f730cd8703c
14 changes: 14 additions & 0 deletions src/Services/SlugService.php
Original file line number Diff line number Diff line change
@@ -303,6 +303,20 @@ protected function makeSlugUnique(string $slug, array $config, string $attribute
}
}

// If uniqueUsing is set to a closure, use it to generate a new "unique" slug
if (isset($config['uniqueUsing']) && is_callable($config['uniqueUsing'])) {
$using = $config['uniqueUsing'];

// Generate mew slug using the closure
$slugUsing = $using($slug, $separator, $list);

// Remove uniqueUsing from the config to avoid infinite loop
unset($config['uniqueUsing']);

// Call makeSlugUnique again with the new slug to ensure it's unique
return $this->makeSlugUnique($slugUsing, $config, $attribute);
}

$method = $config['uniqueSuffix'];
$firstSuffix = $config['firstUniqueSuffix'];