- 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.