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.
#[Sluggable] attribute) while allowing deep customization via the HasSlug trait. This aligns well with Laravel’s Eloquent ecosystem and modern PHP attributes.slug column in the migration (or existing column).-1, -2 suffixes may not suit all use cases (e.g., SEO-sensitive URLs). Custom suffix generators mitigate this.extraScope) can optimize this.HasSlug trait and route adjustments. May conflict with custom route key logic.title → slug, hyphen separator) sufficient, or are closures/translations required?slug column indexed? (Recommended for large datasets.)slug column be added to existing tables without downtime?spatie/laravel-translatable for multilingual slugs.{post:slug}-{id} binding).Phase 1: Basic Slugs (Low Risk)
slug column to target tables (e.g., posts, articles).#[Sluggable] attribute or HasSlug trait to models.create()/update().Phase 2: Self-Healing URLs (Medium Risk)
HasSlug trait and enable selfHealing: true.{model:slug}-{id} binding.308 redirects for stale slugs (handled automatically by the package).Phase 3: Advanced Features (Optional)
generateSlug() to backfill missing slugs.Route::redirect('old-slug', 'new-slug')).BlogPost) to validate integration.Product, Article).$model->generateSlug();
$model->save(); // Or queue this for later.
title changes).#[Sluggable] attribute and HasSlug trait in the team’s style guide.skipGenerateWhen").slug column to speed up collision detection.Model::withoutSlugGeneration(fn () => Model::insert($data));
308) add minimal overhead; cache canonical URLs if needed.slug_cache table to store generated slugs before DB insertion.slug if query performance degrades.| Scenario | Impact | Mitigation |
|---|---|---|
| Slug collision storm | High DB load, duplicate slugs | Implement scoped uniqueness or custom suffix generators. |
| Self-healing misconfiguration | Broken routes (e.g., 404 instead of 308) |
Test route binding thoroughly; use php artisan route:list to verify. |
| Migration failure | Missing slug column |
Add column in a separate migration; backfill slugs post-deployment. |
| Custom suffix generator bug | Infinite loops or invalid slugs | Validate suffixes in tests; log generation attempts. |
| Translatable slugs conflict | Locale-specific collisions | Use extraScope to isolate slugs per locale. |
#[Sluggable] to a model and test slug generation.HasSlug for self-healing).php artisan slug:generate command to backfill slugs (if not using Laravel Boost).How can I help you explore Laravel packages today?