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 Sluggable Laravel Package

spatie/laravel-sluggable

Automatically generate unique, URL-friendly slugs for Laravel Eloquent models on save. Configure slug sources and target fields via a simple HasSlug trait and SlugOptions, with built-in uniqueness handling using Laravel’s Str::slug.

View on GitHub
Deep Wiki
Context7

spatie/laravel-sluggable generates unique URL slugs automatically when saving Eloquent models. Add the HasSlug trait, configure SlugOptions, and persist the result in a dedicated slug column—using Laravel’s Str::slug formatting (spaces become -).

Ideal for posts, products, and other resources that need clean, readable URLs with minimal boilerplate.

  • Auto-generate slugs on model save via a simple trait
  • Ensure uniqueness across records (no collisions)
  • Configure slug source fields and target attribute with SlugOptions
  • Uses Laravel’s native Str::slug for consistent formatting
  • Works with any Eloquent model (just add a migration column)
Frequently asked questions about Laravel Sluggable
How do I install spatie/laravel-sluggable in Laravel?
Run `composer require spatie/laravel-sluggable` to install the package. No additional configuration is needed beyond adding the `HasSlug` trait to your Eloquent models and defining a `slug` column in your database migration.
Which Laravel versions does this package support?
The package supports Laravel 8.x, 9.x, and 10.x. Check the [GitHub repository](https://github.com/spatie/laravel-sluggable) for the latest compatibility details, as minor updates may align with new Laravel releases.
How do I configure slug generation for a specific model?
Use the `getSlugOptions()` method in your model to define the source attribute (e.g., `title`) and customize options like uniqueness handling. For example: `return SlugOptions::create()->source('title')->generateUnique().`
What happens if two models generate the same slug?
By default, the package appends a hyphen and a number (e.g., `-2`) to duplicate slugs. You can override this behavior using `usingSuffixGenerator()` in `SlugOptions` or implement custom logic via `allowDuplicateSlugs()`.
Can I use this package with multi-language content?
Yes, but you’ll need to integrate it with `laravel-translatable` or similar packages. The package itself doesn’t handle translations, so ensure your slug source fields are localized and test thoroughly for performance with large datasets.
How do I backfill slugs for existing records?
Create a migration or queue job to iterate over existing records, generate slugs manually, and update the `slug` column. Avoid doing this in a transaction for large datasets to prevent timeouts; use batch processing instead.
Does this package work with Laravel’s route model binding?
Yes, the generated slugs can be used in route model binding. Ensure your routes use the slug (e.g., `Route::get('/posts/{post:slug}', ...)`) and that your model implements `getRouteKeyName()` to return the `slug` column if needed.
Are there alternatives to spatie/laravel-sluggable for Laravel?
Yes, alternatives include `cviebrock/eloquent-sluggable` and `spatie/laravel-translatable-sluggable` (for multi-language apps). Compare features like uniqueness handling, customization, and Laravel version support before choosing.
How do I test slug generation in PHPUnit?
Mock the `Str::slug` helper or use the package’s `HasSlug` trait directly in tests. Verify slug generation by asserting the `slug` attribute after saving a model. For uniqueness tests, simulate duplicate slugs and confirm the expected suffix is appended.
Can I customize the slug format beyond Str::slug?
Yes, you can override the default `Str::slug` behavior by implementing a custom `sluggable` method in your model or using `usingSuffixGenerator()` to append non-standard suffixes like UUIDs or hashes. This requires extending the `SlugOptions` class.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport