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

Technical Evaluation

Architecture Fit

  • Opinionated but flexible: The package enforces a clean, declarative approach (#[Sluggable] attribute) while allowing deep customization via the HasSlug trait. This aligns well with Laravel’s Eloquent ecosystem and modern PHP attributes.
  • Separation of concerns: Slug generation is decoupled from model logic, reducing boilerplate and improving maintainability.
  • Self-healing URLs: Addresses a critical UX pain point (broken links after title updates) with minimal configuration, making it ideal for content-heavy applications (e.g., CMS, blogs, e-commerce).

Integration Feasibility

  • Low friction: Requires only:
    1. A slug column in the migration (or existing column).
    2. Attribute/trait addition to the model.
    3. Optional: Route binding for self-healing.
  • Backward compatibility: Works with existing Eloquent models without breaking changes.
  • Laravel Boost integration: Reduces onboarding time for teams using AI-assisted development (e.g., Copilot, Cursor).

Technical Risk

  • Minimal: The package is battle-tested (1.5K+ stars, MIT license, active maintenance) with clear documentation. Risks are limited to:
    • Collision handling: Default -1, -2 suffixes may not suit all use cases (e.g., SEO-sensitive URLs). Custom suffix generators mitigate this.
    • Performance: Uniqueness checks add minor overhead during saves. Scoped uniqueness (extraScope) can optimize this.
    • Self-healing URLs: Requires HasSlug trait and route adjustments. May conflict with custom route key logic.
  • Mitigation: Thorough testing in staging, especially for high-traffic models (e.g., products, articles).

Key Questions

  1. Use Case Alignment:
    • Are slugs primarily for SEO, internal routing, or both?
    • Do URLs need to be immutable (e.g., product SKUs) or self-healing (e.g., blog posts)?
  2. Customization Needs:
    • Are default slug rules (e.g., titleslug, hyphen separator) sufficient, or are closures/translations required?
    • Will slugs need to be scoped (e.g., per tenant)?
  3. Performance:
    • How often are models updated? Will uniqueness checks impact throughput?
    • Is the slug column indexed? (Recommended for large datasets.)
  4. Migration Strategy:
    • Can the slug column be added to existing tables without downtime?
    • How will existing URLs be handled during transition (e.g., redirects for stale slugs)?
  5. Tooling:
    • Is Laravel Boost in use? If so, leverage its scaffolding capabilities.
    • Are there existing slug-related utilities (e.g., custom validators) that may conflict?

Integration Approach

Stack Fit

  • Laravel Ecosystem: Native support for Eloquent, routes, and migrations. Complements packages like spatie/laravel-translatable for multilingual slugs.
  • PHP Attributes: Leverages PHP 8+ attributes for concise configuration, reducing boilerplate.
  • Self-Healing URLs: Integrates seamlessly with Laravel’s routing system (e.g., {post:slug}-{id} binding).

Migration Path

  1. Phase 1: Basic Slugs (Low Risk)

    • Add slug column to target tables (e.g., posts, articles).
    • Apply #[Sluggable] attribute or HasSlug trait to models.
    • Test slug generation during create()/update().
    • Impact: Zero downtime; incremental adoption per model.
  2. Phase 2: Self-Healing URLs (Medium Risk)

    • Add HasSlug trait and enable selfHealing: true.
    • Update routes to use {model:slug}-{id} binding.
    • Implement 308 redirects for stale slugs (handled automatically by the package).
    • Impact: Requires route changes; test thoroughly in staging.
  3. Phase 3: Advanced Features (Optional)

    • Custom suffix generators, scoped uniqueness, or translatable slugs.
    • Impact: Minimal; only if specific use cases arise.

Compatibility

  • Laravel Versions: Supports Laravel 10+ (check docs for exact versions).
  • Database: Works with MySQL, PostgreSQL, SQLite (no vendor-specific logic).
  • Existing Slugs: If migrating from a custom slug system:
    • Seed initial slugs via a data migration.
    • Use generateSlug() to backfill missing slugs.
    • Implement redirects for legacy slugs (e.g., Route::redirect('old-slug', 'new-slug')).

Sequencing

  1. Pilot Model: Start with a low-traffic model (e.g., BlogPost) to validate integration.
  2. Core Models: Prioritize models used in public-facing URLs (e.g., Product, Article).
  3. Batch Migration: For large tables, use a queue worker to generate slugs asynchronously:
    $model->generateSlug();
    $model->save(); // Or queue this for later.
    
  4. Monitor: Track slug generation performance and collision rates post-deployment.

Operational Impact

Maintenance

  • Proactive:
    • Monitor slug collision rates (log warnings for duplicates).
    • Review custom suffix generators for edge cases (e.g., infinite loops).
    • Update the package annually to align with Laravel minor versions.
  • Reactive:
    • Slug regeneration is automatic on field updates (e.g., title changes).
    • Self-healing URLs require no manual intervention for title updates.

Support

  • Developer Onboarding:
    • Document the #[Sluggable] attribute and HasSlug trait in the team’s style guide.
    • Provide a runbook for common issues (e.g., "Slug not updating? Check skipGenerateWhen").
  • End-User Impact:
    • Self-healing URLs reduce support tickets about "broken links."
    • Translatable slugs simplify multilingual content management.

Scaling

  • Performance:
    • Uniqueness Checks: Add an index to the slug column to speed up collision detection.
    • Batch Operations: For bulk imports, disable slug generation temporarily or use a queue:
      Model::withoutSlugGeneration(fn () => Model::insert($data));
      
    • Self-Healing: Redirects (308) add minimal overhead; cache canonical URLs if needed.
  • Database:
    • For high-write workloads, consider a slug_cache table to store generated slugs before DB insertion.
    • Partition large tables by slug if query performance degrades.

Failure Modes

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.

Ramp-Up

  • Developer Training:
    • 15-minute demo: Show how to add #[Sluggable] to a model and test slug generation.
    • Hands-on lab: Have engineers implement self-healing URLs for a sample route.
  • Documentation:
    • Create internal docs with:
      • Attribute vs. trait tradeoffs.
      • Common pitfalls (e.g., forgetting HasSlug for self-healing).
      • Performance tuning tips.
  • Tooling:
    • Add a php artisan slug:generate command to backfill slugs (if not using Laravel Boost).
    • Integrate slug validation into feature flags for gradual rollout.
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.
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
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai