The problem with all Slugs getting the same value. #283
Replies: 3 comments 1 reply
-
Hi, I used the basic create() method
|
Beta Was this translation helpful? Give feedback.
-
I got the same problem, very strange... |
Beta Was this translation helpful? Give feedback.
-
I've dug a bit into this since I was having the same issue. In the However, later on when 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 So a simple way to add this functionality to you code base is to create a new Custom Trait that "extends" the base <?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.
-
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?
Beta Was this translation helpful? Give feedback.
All reactions