PHPackages                             whitecube/laravel-sluggable - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. whitecube/laravel-sluggable

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

whitecube/laravel-sluggable
===========================

Generate slugs for your models automatically - even with translated attributes

v1.8.0(1mo ago)2027.2k↓41.1%4[1 issues](https://github.com/whitecube/laravel-sluggable/issues)MITPHPPHP ^8.0|^9.0

Since Feb 7Pushed 1mo ago3 watchersCompare

[ Source](https://github.com/whitecube/laravel-sluggable)[ Packagist](https://packagist.org/packages/whitecube/laravel-sluggable)[ Docs](https://github.com/whitecube/laravel-sluggable)[ GitHub Sponsors](https://github.com/whitecube)[ RSS](/packages/whitecube-laravel-sluggable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (26)Used By (0)

Laravel Sluggable
=================

[](#laravel-sluggable)

A trait to use on your models to generate slugs based on another attribute's value. Supports translated attributes (using [spatie/laravel-translatable](https://github.com/spatie/laravel-translatable)).

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

[](#installation)

You can install the package via composer:

```
composer require whitecube/laravel-sluggable
```

Usage
-----

[](#usage)

```
namespace App;

use Whitecube\Sluggable\HasSlug;

class Post extends Model
{
    use HasSlug;

    public $sluggable = 'title';
}
```

### Changing the slug storage attribute

[](#changing-the-slug-storage-attribute)

By default, the slug is configured to be stored in an attribute named `slug`. You can overwrite this setting with the `public $slugStorageAttribute` property on your model.

```
namespace App;

use Whitecube\Sluggable\HasSlug;

class Post extends Model
{
    use HasSlug;

    public $sluggable = 'title';

    public $slugStorageAttribute = 'custom-slug-column';
}
```

### Conditional sluggable attributes

[](#conditional-sluggable-attributes)

If needed, you can overwrite the trait's `getSluggable()` method and put your own sluggable attribute choice logic in it:

```
namespace App;

use Whitecube\Sluggable\HasSlug;

class Post extends Model
{
    use HasSlug;

    /**
     * Get the attribute name used to generate the slug from
     *
     * @return string
     */
    public function getSluggable()
    {
        return $this->title ? 'title' : 'author';
    }
}
```

### Translated slugs

[](#translated-slugs)

You can generate slugs based on translated attributes (using [spatie/laravel-translatable](https://github.com/spatie/laravel-translatable)). Remember to add the `slug` attribute to the `public $translatable` array to easily access them.

```
namespace App;

use Whitecube\Sluggable\HasSlug;
use Spatie\Translatable\HasTranslations;

class Post extends Model
{
    use HasSlug;
    use HasTranslations;

    public $translatable = ['title', 'slug'];

    public $sluggable = 'title';
}
```

```
Post::create([
    'title' => [
        'en' => 'The title',
        'fr' => 'Le titre'
    ]
]);

$post->getAttributes()['slug']; // ['en' => 'the-title', 'fr' => 'le-titre']
$post->slug; // the-title (given that the crrent app locale is 'en')
$post->translate('slug', 'fr'); // 'le-titre'
```

Route Model Binding
-------------------

[](#route-model-binding)

Be advised that this package overrides the `getRouteKeyName` method, which means [Laravel's Route Model Binding](https://laravel.com/docs/master/routing#route-model-binding) will use the slug attribute by default (or the `$slugStorageAttribute` you have defined). In most cases, this is great, saves you a step and cleans up your models, but if you must, you can change it to whatever you like.

When using Route Model Binding, some other interesting features become available.

### Custom Route Model Binding Query

[](#custom-route-model-binding-query)

Sometimes you'd want to add custom behavior to the query Laravel performs. This packages makes it possible to hook into the query by defining a `getRouteBindingQuery` method on the model, which could be useful in several cases such as including soft-deleted results :

```
namespace App;

use Whitecube\Sluggable\HasSlug;

class Post extends Model
{
    use HasSlug;

    // ...

    protected function getRouteBindingQuery($query) {
        return $query->withTrashed();
    }
}
```

### Cross-language redirects

[](#cross-language-redirects)

If the slug provided in the URL does not correspond to the current locale's slug translation, but corresponds to a slug in another language, this package can automatically redirect to the proper slug.

An example: given the above example's post, we could imagine the following routing configuration:

```
/en/posts/the-title
/fr/articles/le-titre

```

But if we visit `/fr/articles/the-title`, the package will automatically perform a `301` redirect to `/fr/articles/le-titre`.

This behavior can be disabled by setting `public $slugTranslationRedirect = false;` on your model, in which case visiting `/fr/articles/the-title` will just render a `404` page.

### Translated URL generator

[](#translated-url-generator)

Do you need to generate alternate locale links for a translated sluggable model? We've got you covered. Take a look at `getSluggedUrlForRoute($route, $locale, $fullUrl)`:

```
$post = Post::create([
    'title' => [
        'en' => 'The title in English',
        'fr' => 'Le titre en Français'
    ]
]);

$alternates = [];

foreach (['en', 'fr'] as $locale) {
    $alternates[] = $post->getSluggedUrlForRoute(Route::current(), $locale);
}
```

Considering the current route being `/blog/{post}/{tab}` with request parameter `{tab} = 'comments'`, the result would be:

```
/blog/the-title-in-english/comments
/blog/le-titre-en-francais/comments

```

💖 Sponsorships
--------------

[](#-sponsorships)

If you are reliant on this package in your production applications, consider [sponsoring us](https://github.com/sponsors/whitecube)! It is the best way to help us keep doing what we love to do: making great open source software.

Contributing
------------

[](#contributing)

Feel free to suggest changes, ask for new features or fix bugs yourself. We're sure there are still a lot of improvements that could be made, and we would be very happy to merge useful pull requests.

Thanks!

Made with ❤️ for open source
----------------------------

[](#made-with-️-for-open-source)

At [Whitecube](https://www.whitecube.be) we use a lot of open source software as part of our daily work. So when we have an opportunity to give something back, we're super excited!

We hope you will enjoy this small contribution from us and would love to [hear from you](mailto:hello@whitecube.be) if you find it useful in your projects. Follow us on [Twitter](https://twitter.com/whitecube_be) for more updates!

###  Health Score

61

—

FairBetter than 99% of packages

Maintenance88

Actively maintained with recent releases

Popularity37

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity84

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 55.3% 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 ~101 days

Recently: every ~174 days

Total

23

Last Release

56d ago

PHP version history (4 changes)1.0.0PHP ^7.1

v1.5.0PHP ^7.1|^8.0

v1.6.0PHP ^7.1|^8.0|^9.0

v1.6.4PHP ^8.0|^9.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8435657?v=4)[Whitecube](/maintainers/Whitecube)[@whitecube](https://github.com/whitecube)

---

Top Contributors

[![voidgraphics](https://avatars.githubusercontent.com/u/9298484?v=4)](https://github.com/voidgraphics "voidgraphics (26 commits)")[![toonvandenbos](https://avatars.githubusercontent.com/u/5635557?v=4)](https://github.com/toonvandenbos "toonvandenbos (16 commits)")[![jan-tricks](https://avatars.githubusercontent.com/u/113358501?v=4)](https://github.com/jan-tricks "jan-tricks (4 commits)")[![theokbokki](https://avatars.githubusercontent.com/u/90846584?v=4)](https://github.com/theokbokki "theokbokki (1 commits)")

---

Tags

laravel-sluggablewhitecube

### Embed Badge

![Health badge](/badges/whitecube-laravel-sluggable/health.svg)

```
[![Health](https://phpackages.com/badges/whitecube-laravel-sluggable/health.svg)](https://phpackages.com/packages/whitecube-laravel-sluggable)
```

###  Alternatives

[illuminate/pipeline

The Illuminate Pipeline package.

9346.6M213](/packages/illuminate-pipeline)[whitecube/laravel-cookie-consent

Register, configure and ask for cookies consent in a EU-compliant way.

489563.9k1](/packages/whitecube-laravel-cookie-consent)[whitecube/laravel-timezones

Store UTC dates in the database and work with custom timezones in the application.

106106.2k](/packages/whitecube-laravel-timezones)[interaction-design-foundation/laravel-geoip

Support for multiple Geographical Location services.

17221.0k3](/packages/interaction-design-foundation-laravel-geoip)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)[whitecube/laravel-prices

Manage acquisition, selling &amp; renting prices for products and services in your Laravel Application

121.6k](/packages/whitecube-laravel-prices)

PHPackages © 2026

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