PHPackages                             jetcod/laravel-slugify - 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. jetcod/laravel-slugify

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

jetcod/laravel-slugify
======================

A Laravel package that automatically generates slugs for model attributes, simplifying the creation of URL-friendly slugs.

1.3.0(7mo ago)686MITPHPPHP ^7.4|^8.0CI passing

Since Oct 27Pushed 7mo ago1 watchersCompare

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

READMEChangelog (3)Dependencies (6)Versions (6)Used By (0)

Laravel Slugify Package
=======================

[](#laravel-slugify-package)

[![Build](https://camo.githubusercontent.com/8e9521ea4195f9d05ffc54a55fa4f74a4b5b60d6c3772ef745e27243699d0a8b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6a6574636f642f6c61726176656c2d736c75676966792f74657374732e796d6c3f7374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/8e9521ea4195f9d05ffc54a55fa4f74a4b5b60d6c3772ef745e27243699d0a8b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6a6574636f642f6c61726176656c2d736c75676966792f74657374732e796d6c3f7374796c653d666f722d7468652d6261646765)

[![Latest Stable Version](https://camo.githubusercontent.com/614bdd6e55cb75e783569516bb5558e4c8a4a497bb6f04a4580e9f0b12a81269/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6574636f642f6c61726176656c2d736c75676966793f6c6162656c3d4c6174657374253230537461626c6525323056657273696f6e)](https://packagist.org/packages/jetcod/laravel-slugify)[![Total Downloads](https://camo.githubusercontent.com/582f4ba5fb5a8635b0d3ca3ac82677585c8cd03599d1d6d572bcd4b4cc58f0f0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a6574636f642f6c61726176656c2d736c75676966793f6c6162656c3d546f74616c253230446f776e6c6f616473)](https://packagist.org/packages/jetcod/laravel-slugify)[![License](https://camo.githubusercontent.com/d5b1169045ef041ce90ffffa0ef64c249391d025c42349851ce60022486898ce/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6a6574636f642f6c61726176656c2d736c75676966793f6c6162656c3d4c6963656e7365)](https://github.com/jetcod/laravel-slugify/blob/main/LICENSE)

Overview
--------

[](#overview)

The `jetcod\laravel-slugify` package simplifies the generation and management of slugs for Eloquent models in Laravel applications. This package utilizes the `HasSlug` trait to automatically create and update slugs based on your model's attributes, with flexible configuration options.

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

[](#installation)

### Requirements

[](#requirements)

- PHP ^7.4 | ^8.0
- Laravel 8.0+

### Step 1: Install via Composer

[](#step-1-install-via-composer)

To install the package, run the following command:

```
composer require jetcod/laravel-slugify
```

### Step 2: Configure your model

[](#step-2-configure-your-model)

In any model where you want to use the slugging functionality, use the `HasSlug` trait and implement `getSlugConfig()` method to configure the slug options.

Usage
=====

[](#usage)

Setting Up the Sluggable Model
------------------------------

[](#setting-up-the-sluggable-model)

To start using slugs in your model, follow these steps:

- **Use the Trait**: In your model, use the `HasSlug` trait.
- **Define Slug Configuration**: Implement the `getSlugConfig()` method in your model. This method should return a SlugOptions object where you define your sluggable configuration.
- **Set the `$sluggables` property**: Define an array of model attributes that should be used to generate the slug.

Here’s an example:

```
use Jetcod\LaravelSlugify\SlugOptions;
use Jetcod\LaravelSlugify\Traits\HasSlug;

class YourModel extends Model
{
    use HasSlug;

    protected $casts = [
        'slugs' => 'array', // Optional: Cast the 'slugs' attribute to an array
    ];

    protected $sluggables = ['a_column_name'];  // Specify columns to be sluggified

    protected function getSlugConfig(): SlugOptions
    {
        return SlugOptions::create()
            ->slugColumn('slugs')   // Define the column name where the slugs will be stored
        ;
    }
}
```

Available Configuration Options
-------------------------------

[](#available-configuration-options)

- **doNotGenerateSlugsOnCreate()**: Call this method if you want to avoid generating slugs on model creation.
- **doNotGenerateSlugsOnUpdate()**: Call this method if you want to avoid generating slugs when the model is updated.
- **slugColumn(string)**: Define the column name where the slugs will be stored. The defined column type should be `json`.
- **slugSeparator(string)**: Character separator between words in the slug. (Default value is '-')
- **maximumLength(int)**: Maximum character length of the slug.
- **avoidDuplicates()**: Call this method to generate unique slugs.

> **Note**: Calling slugColumn() is required while defining the slug options.

**Example**: Creating and Updating a Model with Slug Generation
---------------------------------------------------------------

[](#example-creating-and-updating-a-model-with-slug-generation)

When you create or update a model with HasSlug configured, the slug is automatically generated and saved according to the options you've specified.

```
$post = new Post();
$post->title = "This is an Example Title";
$post->save();

// $post->slugs will be like {"title":"this-is-an-example-title"} (based on the configured options)
```

**Example**: Generating Unique Slugs
------------------------------------

[](#example-generating-unique-slugs)

To ensure unique slugs, you can use the `avoidDuplicates()` method in your SlugOptions:

```
use Jetcod\LaravelSlugify\SlugOptions;
use Jetcod\LaravelSlugify\Traits\HasSlug;

class YourModel extends Model
{
    use HasSlug;

    protected $sluggables = ['title'];

    protected function getSlugConfig(): SlugOptions
    {
        return SlugOptions::create()
            ->slugColumn('slugs')
            ->avoidDuplicates()
        ;
    }
}
```

Then when you create or update a model, the slug will be generated and saved as a unique value.

```
$post = new Post();
$post->title = "This is an Example Title";
$post->save();

// Result: $post->slugs will be like {"title":"this-is-an-example-title"}

// Create another post with the same title
$post = new Post();
$post->title = "This is an Example Title";
$post->save();

// Result: $post->slugs will be like {"title":"this-is-an-example-title-1"}
```

> **Note**: It also considers the maximum length of the slug while generating unique slug strings.

Testing
-------

[](#testing)

Run your tests to verify that slugs are generated as expected:

```
composer test
```

Run code coverage analysis to generate a coverage report. This will generate a coverage report in the `coverage` directory.

```
composer coverage
```

Run PHPStan to check for potential issues in the code:

```
composer phpstan
```

License
-------

[](#license)

This package is open-source software licensed under the [MIT License](LICENSE).

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance64

Regular maintenance activity

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

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 ~113 days

Total

4

Last Release

222d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4dedaadbfaf607c33c4b02a55eb9e3a4cf5407267d99d4b4eb7b3fe4a4f1e327?d=identicon)[hamidgh83](/maintainers/hamidgh83)

---

Top Contributors

[![hamidgh83](https://avatars.githubusercontent.com/u/3264788?v=4)](https://github.com/hamidgh83 "hamidgh83 (36 commits)")

---

Tags

urlslugslugifylaravelurl-friendlyurl-slugurl-slugifyurl-slugify-laravellaravel-slugify

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jetcod-laravel-slugify/health.svg)

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

###  Alternatives

[jbroadway/urlify

A fast PHP slug generator and transliteration library that converts non-ascii characters for use in URLs.

6737.4M62](/packages/jbroadway-urlify)[cybercog/laravel-optimus

An Optimus bridge for Laravel. Id obfuscation based on Knuth's multiplicative hashing method.

192564.1k](/packages/cybercog-laravel-optimus)[keyvanakbary/slugifier

A full-featured, simple, clean and pure functional implementation for creating slugs

68187.9k4](/packages/keyvanakbary-slugifier)[dusterio/link-preview

Link preview generation for PHP with Laravel support

126326.6k3](/packages/dusterio-link-preview)[laracrafts/laravel-url-shortener

Powerful URL shortening tools in Laravel

97110.7k](/packages/laracrafts-laravel-url-shortener)

PHPackages © 2026

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