PHPackages                             philiprehberger/laravel-slug-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. philiprehberger/laravel-slug-generator

ActiveLibrary[Database &amp; ORM](/categories/database)

philiprehberger/laravel-slug-generator
======================================

Automatic slug generation for Eloquent models with scoped uniqueness, history, and transliteration

v1.0.2(1mo ago)15[2 PRs](https://github.com/philiprehberger/laravel-slug-generator/pulls)MITPHPPHP ^8.2CI passing

Since Mar 9Pushed 1mo agoCompare

[ Source](https://github.com/philiprehberger/laravel-slug-generator)[ Packagist](https://packagist.org/packages/philiprehberger/laravel-slug-generator)[ Docs](https://github.com/philiprehberger/laravel-slug-generator)[ RSS](/packages/philiprehberger-laravel-slug-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (10)Versions (4)Used By (0)

Laravel Slug Generator
======================

[](#laravel-slug-generator)

[![Tests](https://github.com/philiprehberger/laravel-slug-generator/actions/workflows/tests.yml/badge.svg)](https://github.com/philiprehberger/laravel-slug-generator/actions/workflows/tests.yml)[![Latest Version on Packagist](https://camo.githubusercontent.com/26d700814bb147a909a65b1330ec9a1e56b059a86f4b339439d6769dbfc76c0c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7068696c69707265686265726765722f6c61726176656c2d736c75672d67656e657261746f722e737667)](https://packagist.org/packages/philiprehberger/laravel-slug-generator)[![License](https://camo.githubusercontent.com/d5f9c5300948e3198b8c8854e6be3f38fedad93a3713228f31dfd565f8f08d3e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7068696c69707265686265726765722f6c61726176656c2d736c75672d67656e657261746f72)](LICENSE)

Automatic slug generation for Eloquent models with scoped uniqueness, history, and transliteration.

Requirements
------------

[](#requirements)

- PHP 8.2+
- Laravel 11 or 12

The PHP `intl` extension is recommended for best transliteration results (falls back to `iconv` then a simple strip).

Installation
------------

[](#installation)

```
composer require philiprehberger/laravel-slug-generator
```

The service provider is auto-discovered via Laravel's package discovery. No manual registration is required.

### Publish configuration

[](#publish-configuration)

```
php artisan vendor:publish --tag=slug-generator-config
```

This creates `config/slug-generator.php` in your application.

### Publish and run the slug history migration (optional)

[](#publish-and-run-the-slug-history-migration-optional)

Only required if you intend to use the `HasSlugHistory` trait.

```
php artisan vendor:publish --tag=slug-generator-migrations
php artisan migrate
```

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

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'
```

### Per-Model Overrides

[](#per-model-overrides)

```
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; }
}
```

### Scoped Uniqueness

[](#scoped-uniqueness)

```
class Post extends Model
{
    use HasSlug;

    public function slugUniqueScope(): ?string
    {
        return 'category_id';
    }
}
```

### Slug History and Redirects

[](#slug-history-and-redirects)

```
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]);
}
```

API
---

[](#api)

### HasSlug Trait — Override Methods

[](#hasslug-trait--override-methods)

MethodReturn TypeDefaultDescription`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`slugOnUpdate()``bool``false`Regenerate slug on model update### HasSlugHistory Trait

[](#hasslughistory-trait)

MethodDescription`Post::findBySlugOrRedirect(string $slug)`Returns model, redirect array, or null`->slugHistories`MorphMany relationship to slug history records### SlugRedirectMiddleware Parameters

[](#slugredirectmiddleware-parameters)

PositionNameDescription1`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: `/`)Development
-----------

[](#development)

```
composer install
vendor/bin/phpunit
vendor/bin/pint --test
vendor/bin/phpstan analyse
```

License
-------

[](#license)

MIT

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance89

Actively maintained with recent releases

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~4 days

Total

3

Last Release

56d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cfd7d24cbbf32400fa13ce0bbe7a31edd2d66a6d4488eafdb3d64c5337bf0435?d=identicon)[philiprehberger](/maintainers/philiprehberger)

---

Top Contributors

[![philiprehberger](https://avatars.githubusercontent.com/u/8218077?v=4)](https://github.com/philiprehberger "philiprehberger (18 commits)")

---

Tags

urlsluglaraveleloquentseo

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/philiprehberger-laravel-slug-generator/health.svg)

```
[![Health](https://phpackages.com/badges/philiprehberger-laravel-slug-generator/health.svg)](https://phpackages.com/packages/philiprehberger-laravel-slug-generator)
```

###  Alternatives

[cviebrock/eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel

4.0k13.6M253](/packages/cviebrock-eloquent-sluggable)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[jerome/filterable

Streamline dynamic Eloquent query filtering with seamless API request integration and advanced caching strategies.

19226.1k](/packages/jerome-filterable)[sirodiaz/laravel-redirection

Laravel package that allows storing in database (or other sources) urls to redirect for SEO purposes

6316.0k](/packages/sirodiaz-laravel-redirection)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
