PHPackages                             bpocallaghan/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. [Database &amp; ORM](/categories/database)
4. /
5. bpocallaghan/sluggable

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

bpocallaghan/sluggable
======================

Provides a HasSlug trait that will generate a unique slug when saving your Laravel Eloquent model.

1.6.1(3mo ago)622.3k↓48.5%2[1 PRs](https://github.com/bpocallaghan/sluggable/pulls)3MITPHPPHP ^8.2

Since Jul 7Pushed 3mo ago3 watchersCompare

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

READMEChangelog (9)Dependencies (3)Versions (10)Used By (3)

Generate slugs when saving Laravel Eloquent models
==================================================

[](#generate-slugs-when-saving-laravel-eloquent-models)

Provides a HasSlug trait that will generate a unique slug when saving your Laravel Eloquent model.

The slugs are generated with Laravel `str_slug` method, whereby spaces are converted to '-'.

```
$model = new EloquentModel();
$model->name = 'laravel is awesome';
$model->save();

echo $model->slug; // outputs "laravel-is-awesome"
```

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

[](#installation)

Update your project's `composer.json` file.

```
composer require bpocallaghan/sluggable
```

Usage
-----

[](#usage)

Your Eloquent models can use the `Bpocallaghan\Sluggable\HasSlug` trait and the `Bpocallaghan\Sluggable\SlugOptions` class.

The trait has a protected method `getSlugOptions()` that you can implement for customization.

Here's an example:

```
class YourEloquentModel extends Model
{
    use HasSlug;

    /**
     * This function is optional and only required
     * when you want to override the default behaviour
     */
    protected function getSlugOptions()
    {
        return SlugOptions::create()
            ->slugSeperator('-')
            ->generateSlugFrom('name')
            ->saveSlugTo('slug');
    }
}
```

If you want to generate your slug from a relationship.

```
class YourEloquentModel extends Model
{
    use HasSlug;

    public function getNameAndFooAttribute()
    {
        $name = $this->name;
        if ($this->foo) {
            $name .= " {$this->foo->name}";
        }

        return $name;
    }

    protected function getSlugOptions()
    {
        return SlugOptions::create()
            ->generateSlugFrom('name_and_foo');
    }
}
```

Config
------

[](#config)

You do not have to add the method in you model (the above will be used as default).

It is only needed when you want to change the default behaviour.

By default it will generate a slug from the `name` and save to the `slug` column.

It will suffix a `-1` to make the slug unique. You can disable it by calling `makeSlugUnique(false)`.

It will use the `-` as a separator. You can change this by calling `slugSeperator('_')`.

You can use multiple fields as the source of the slug `generateSlugFrom(['firstname', 'lastname'])`.

You can also pass a `callable` function to `generateSlugFrom()`.

Have a look [here for the options](https://github.com/bpocallaghan/sluggable/blob/master/src/SlugOptions.php) and available config functions.

###  Health Score

57

—

FairBetter than 98% of packages

Maintenance80

Actively maintained with recent releases

Popularity33

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity81

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 85.2% 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 ~391 days

Recently: every ~747 days

Total

9

Last Release

106d ago

PHP version history (3 changes)1.0.0PHP &gt;=5.6.4

1.3.1PHP ^7.2

1.5PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/7cc52fc6eaeee1d7cbb729f7b7e7fb570754f4d2bb57847e5753f4e1041b7124?d=identicon)[bpocallaghan](/maintainers/bpocallaghan)

---

Top Contributors

[![bpocallaghan](https://avatars.githubusercontent.com/u/883465?v=4)](https://github.com/bpocallaghan "bpocallaghan (23 commits)")[![cblauth](https://avatars.githubusercontent.com/u/79920782?v=4)](https://github.com/cblauth "cblauth (4 commits)")

---

Tags

hacktoberfesthacktoberfest2022sluglaraveleloquentsluggableunique

### Embed Badge

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

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

###  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)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[toponepercent/baum

Baum is an implementation of the Nested Set pattern for Eloquent models.

3154.7k](/packages/toponepercent-baum)

PHPackages © 2026

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