Automatic slug generation for Eloquent models with scoped uniqueness, history, and transliteration
composer require philiprehberger/laravel-slug-generatorAutomatic slug generation for Eloquent models with scoped uniqueness, history, and transliteration.
The PHP intl extension is recommended for best transliteration results (falls back to iconv then a simple strip).
composer require philiprehberger/laravel-slug-generator
The service provider is auto-discovered via Laravel's package discovery. No manual registration is required.
php artisan vendor:publish --tag=slug-generator-config
This creates config/slug-generator.php in your application.
Only required if you intend to use the HasSlugHistory trait.
php artisan vendor:publish --tag=slug-generator-migrations
php artisan migrate
Add the HasSlug trait to any Eloquent model:
use Illuminate\Database\Eloquent\Model;
use PhilipRehberger\SlugGenerator\Concerns\HasSlug;
class Post extends Model
{
use HasSlug;
}
The trait reads from the title column by default and writes to slug:
$post = Post::create(['title' => 'Hello World']);
echo $post->slug; // 'hello-world'
Duplicate slugs automatically receive a numeric suffix:
Post::create(['title' => 'Hello World']); // slug: 'hello-world'
Post::create(['title' => 'Hello World']); // slug: 'hello-world-2'
class Article extends Model
{
use HasSlug;
public function slugSource(): string|array { return 'title'; }
public function slugField(): string { return 'slug'; }
public function slugSeparator(): string { return '-'; }
public function slugMaxLength(): ?int { return null; }
public function slugShouldBeUnique(): bool { return true; }
public function slugUniqueScope(): ?string { return null; }
public function slugOnUpdate(): bool { return false; }
}
class Post extends Model
{
use HasSlug;
public function slugUniqueScope(): ?string
{
return 'category_id';
}
}
Use a template pattern to control how attributes are combined in the slug:
class Author extends Model
{
use HasSlug;
public function slugTemplate(): ?string
{
return '{last_name}-{first_name}';
}
}
$author = Author::create(['first_name' => 'John', 'last_name' => 'Doe']);
echo $author->slug; // 'doe-john'
Placeholders use the {attribute} syntax and are resolved from model attributes before slugification. Missing or null attributes are omitted from the result.
use PhilipRehberger\SlugGenerator\Concerns\HasSlug;
use PhilipRehberger\SlugGenerator\Concerns\HasSlugHistory;
class Post extends Model
{
use HasSlug;
use HasSlugHistory;
public function slugOnUpdate(): bool { return true; }
}
Use findBySlugOrRedirect() in controllers to handle current and old slugs transparently:
public function show(string $slug): Response
{
$result = Post::findBySlugOrRedirect($slug);
if ($result === null) { abort(404); }
if (is_array($result) && $result['redirect']) {
return redirect(route('posts.show', $result['slug']), 301);
}
return view('posts.show', ['post' => $result]);
}
| Method | Return Type | Default | Description |
|---|---|---|---|
slugSource() | string|array | 'title' | Source column(s) to generate slug from |
slugField() | string | 'slug' | Database column to store the slug |
slugSeparator() | string | '-' | Word separator |
slugMaxLength() | ?int | null | Max length; truncates at word boundary |
slugShouldBeUnique() | bool | true | Enforce unique slugs |
slugUniqueScope() | ?string | null | Column to scope uniqueness checks |
slugTemplate() | ?string | null | Template pattern with {attribute} placeholders |
slugOnUpdate() | bool | false | Regenerate slug on model update |
| Method | Description |
|---|---|
Post::findBySlugOrRedirect(string $slug) | Returns model, redirect array, or null |
->slugHistories | MorphMany relationship to slug history records |
| Position | Name | Description |
|---|---|---|
| 1 | modelClass | Fully-qualified model class (must use HasSlugHistory) |
| 2 | routeParam | Route parameter name that holds the slug (default: slug) |
| 3 | urlPrefix | URL prefix for the redirect target (default: /) |
composer install
vendor/bin/phpunit
vendor/bin/pint --test
vendor/bin/phpstan analyse
If you find this project useful: