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(5mo ago)624.2k↓26.6%2[1 PRs](https://github.com/bpocallaghan/sluggable/pulls)3MITPHPPHP ^8.2

Since Jul 7Pushed 5mo ago3 watchersCompare

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

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

55

—

FairBetter than 97% of packages

Maintenance73

Regular maintenance activity

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

151d 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

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M96](/packages/mongodb-laravel-mongodb)[cviebrock/eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel

4.0k14.3M286](/packages/cviebrock-eloquent-sluggable)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k32.6M46](/packages/kirschbaum-development-eloquent-power-joins)[spatie/laravel-sluggable

Generate slugs when saving Eloquent models

1.6k12.9M311](/packages/spatie-laravel-sluggable)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)

PHPackages © 2026

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