Skip to content

Commit c0159d8

Browse files
translation unique slug
1 parent c7ec358 commit c0159d8

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed

packages/tag/composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
},
2323
"autoload": {
2424
"psr-4": {
25-
"Moox\\Tag\\": "src",
26-
"Moox\\Tag\\Database\\Factories\\": "database/factories",
27-
"Moox\\Tag\\Database\\Seeders\\": "database/seeders"
25+
"Moox\\Tag\\": "src/"
2826
}
2927
},
3028
"extra": {

packages/tag/database/migrations/create_tag_translations.php.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ return new class extends Migration
2020
$table->text('content')->nullable();
2121

2222
// Ensure slug is unique per locale
23-
$table->unique(['slug', 'locale'], 'unique_slug_per_locale');
23+
$table->unique(['slug', 'locale']);
2424

2525
// Ensure one translation per locale per tag
26-
$table->unique(['tag_id', 'locale'], 'unique_translation_per_locale');
26+
$table->unique(['tag_id', 'locale']);
2727

2828
// Foreign key constraint
2929
$table->foreign('tag_id')

packages/tag/src/Models/Tag.php

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,47 @@ class Tag extends Model implements HasMedia, TranslatableContract
4545
public function fillTranslations(array $translations): self
4646
{
4747
foreach ($translations as $locale => $data) {
48-
if (! empty($data['title'])) {
48+
if (!empty($data['title'])) {
49+
// Get the translation for this locale (or create a new one)
50+
$translation = $this->translateOrNew($locale);
51+
52+
// Check if title has changed
53+
if ($translation->title !== $data['title']) {
54+
$translation->title = $data['title'];
55+
}
56+
57+
// Handle the slug only if it has changed
4958
$slug = $data['slug'] ?? Str::slug($data['title']);
50-
51-
// Ensure slug uniqueness per locale
52-
$slug = $this->generateUniqueSlug($slug, $locale);
53-
54-
$this->translateOrNew($locale)->fill([
55-
'title' => $data['title'],
56-
'slug' => $slug,
57-
'content' => $data['content'] ?? null,
58-
]);
59+
if ($translation->slug !== $slug) {
60+
$slug = $this->generateUniqueSlug($slug, $locale);
61+
$translation->slug = $slug;
62+
}
63+
64+
// Handle content only if it has changed
65+
if ($translation->content !== ($data['content'] ?? null)) {
66+
$translation->content = $data['content'] ?? null;
67+
}
68+
69+
$translation->save();
5970
}
6071
}
61-
72+
6273
return $this;
6374
}
64-
65-
private function generateUniqueSlug(string $slug, string $locale): string
75+
public function generateUniqueSlug(string $slug, string $locale, int $counter = 0): string
6676
{
67-
$originalSlug = $slug;
68-
$count = 1;
69-
70-
while (TagTranslation::where('slug', $slug)->where('locale', $locale)->exists()) {
71-
$slug = $originalSlug.'-'.$count;
72-
$count++;
73-
}
74-
75-
return $slug;
77+
// Append counter if needed
78+
$uniqueSlug = $counter > 0 ? "{$slug}-{$counter}" : $slug;
79+
80+
// Check if the slug exists for this locale
81+
$exists = static::whereHas('translations', function ($query) use ($uniqueSlug, $locale) {
82+
$query->where('slug', $uniqueSlug)->where('locale', $locale);
83+
})->exists();
84+
85+
// If exists, try again with an incremented counter
86+
return $exists ? $this->generateUniqueSlug($slug, $locale, $counter + 1) : $uniqueSlug;
7687
}
77-
88+
7889
/**
7990
* Get all translations as a formatted array
8091
*/

0 commit comments

Comments
 (0)