Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Sluggable Laravel Package

spatie/laravel-sluggable

Generate unique slugs for Eloquent models on create/update. Supports collision suffixes, translatable slugs, and customizable slug options. Includes self-healing URLs that keep old links working via slug+ID route keys with 308 redirects to the canonical URL.

View on GitHub
Deep Wiki
Context7

title: Translatable slugs weight: 5

Use the HasTranslatableSlug trait together with spatie/laravel-translatable to store one slug per locale.

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Spatie\Sluggable\HasTranslatableSlug;
use Spatie\Sluggable\SlugOptions;
use Spatie\Translatable\HasTranslations;

class Article extends Model
{
    use HasTranslations;
    use HasTranslatableSlug;

    public $translatable = ['title', 'slug'];

    public function getSlugOptions(): SlugOptions
    {
        return SlugOptions::create()
            ->generateSlugsFrom('title')
            ->saveSlugsTo('slug');
    }
}

The slug column must hold a JSON value; every translatable attribute in $translatable (including the slug itself) is stored as a locale-keyed object.

Generating slugs from a callable per locale

When using a callable, instantiate SlugOptions with createWithLocales(). The callable receives the model and the current locale.

public function getSlugOptions(): SlugOptions
{
    return SlugOptions::createWithLocales(['en', 'nl'])
        ->generateSlugsFrom(fn (Article $article, string $locale) => "{$locale} {$article->id}")
        ->saveSlugsTo('slug');
}

Routing

Use Laravel's implicit route model binding, pointing at the slug column.

Route::get('/articles/{article:slug}', fn (Article $article) => $article);

HasTranslatableSlug handles the locale-aware JSON lookup in resolveRouteBindingQuery().

Finding models by slug

HasTranslatableSlug ships its own findBySlug() that searches the current locale and the application fallback locale.

$article = Article::findBySlug('my-article');

Self-healing URLs

selfHealing() works with the translatable trait. The route key uses the slug for the current locale; stale slugs trigger the same redirect flow described in Self-healing URLs.

Getting the route key for a specific locale

Use getLocalizedRouteKey() to retrieve the route key for a given locale without permanently changing the model's active locale.

$article->getLocalizedRouteKey('nl'); // "nederlandse-titel-5"

The method swaps in the requested locale, calls getRouteKey(), and restores the original locale via try/finally, so the model is always left as it was found, including when getRouteKey() throws.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours