-
-
Notifications
You must be signed in to change notification settings - Fork 188
The problem with all Slugs getting the same value. #283
-
public function getSlugOptions(): SlugOptions { return SlugOptions::createWithLocales(['tr', 'en']) ->generateSlugsFrom(function($model, $locale) { return "{$locale}{$model->id}"; }) ->saveSlugsTo('slug'); }
Hello, I am using the Spatie/translatable and spatie/sluggable packages together in my administration panel that I developed with the Laravel/Filament package. But, I use the above code in the model when generating slug according to the language. But when I update, all languages get the same slug value. How can I solve this problem?
{"tr":"en-16","en":"en-16"}
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 3 comments 1 reply
-
Hi,
Same issue here, the slug is initially setup with the same values and does not update even when I force it manually;
I used the basic create() method
public function getSlugOptions(): SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom('title')
->saveSlugsTo('slug');
}
Beta Was this translation helpful? Give feedback.
All reactions
-
I got the same problem, very strange...
Beta Was this translation helpful? Give feedback.
All reactions
-
I've dug a bit into this since I was having the same issue.
In the HasTranslatableSlug
trait, the addSlug
method calls $this->withLocale(...)
to set the locale at the application level through the Illuminate\Support\Traits\Localizable
trait. This sets the locale at the laravel container level.
However, later on when getSlugSourceStringFromCallable
is called in HasTranslatableSlug
, it calls $this->getLocale()
to retrieve the locale that is based to the callable in generateSlugsFrom
. This call to $this->getLocale()
gets the locale set at the level of the model by the translatable package in HasTranslations
, but the $translationLocale
defined in HasTranslations
is not set by the call to withLocale(...)
, so it uses the initial locale set on the request.
I'm not entirely sure what the intention is with the code. For a simple fix, which I'm not sure would be the way forward since it seems to negate the need for the call to withLocale
, is to add a call the $this->setLocale($locale)
method from HasTranslations
inside the withLocale(...)
call.
So a simple way to add this functionality to you code base is to create a new Custom Trait that "extends" the base hasTranslatableSlug
trait:
<?php namespace App\Models\Traits; use Spatie\Sluggable\HasTranslatableSlug; trait CustomHasTranslatableSlug { use HasTranslatableSlug { addSlug as originalAddSlug; } protected function addSlug(): void { $this->ensureValidSlugOptions(); $this->getLocalesForSlug()->unique()->each(function ($locale) { $this->withLocale($locale, function () use ($locale) { $this->setLocale($locale); // Extra line to set the locale on the model $slug = $this->generateNonUniqueSlug(); $slugField = $this->slugOptions->slugField; if ($this->slugOptions->generateUniqueSlugs) { // temporarly change the 'slugField' of the SlugOptions // so the 'otherRecordExistsWithSlug' method queries // the locale JSON column instead of the 'slugField'. $this->slugOptions->saveSlugsTo("{$slugField}->{$locale}"); $slug = $this->makeSlugUnique($slug); // revert the change for the next iteration $this->slugOptions->saveSlugsTo($slugField); } $this->setTranslation($slugField, $locale, $slug); }); }); } }
And then use that trait in your model instead.
Beta Was this translation helpful? Give feedback.
All reactions
-
I will test your suggestion and get back to you, but this method seems not to be very useful. Instead, a pull request can be made after testing this method. Then it can be added as a setting to the config file.
Beta Was this translation helpful? Give feedback.