PHPackages                             oddvalue/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. oddvalue/laravel-sluggable

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

oddvalue/laravel-sluggable
==========================

Generate slugs when saving Eloquent models

2.6.0(5y ago)011MITPHPPHP ^7.4|^8.0

Since Dec 24Pushed 5y agoCompare

[ Source](https://github.com/oddvalue/laravel-sluggable)[ Packagist](https://packagist.org/packages/oddvalue/laravel-sluggable)[ Docs](https://github.com/spatie/laravel-sluggable)[ RSS](/packages/oddvalue-laravel-sluggable/feed)WikiDiscussions 2.2 Synced today

READMEChangelogDependencies (5)Versions (36)Used By (0)

**Fork of  to allow use on L8 with PHP 7.3, unless you need that use the original package**

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

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

[![Latest Version on Packagist](https://camo.githubusercontent.com/111f515bb7625efc36aa9c13df296f49150279b00e6c9f54fbb19dab37be329e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f646476616c75652f6c61726176656c2d736c75676761626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/oddvalue/laravel-sluggable)[![MIT Licensed](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/77f472cbc6bb4416fc7e94d5d41ad758e0ddc398fad4ab8f183378603ef4ce1f/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7370617469652f6c61726176656c2d736c75676761626c652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/spatie/laravel-sluggable)[![Quality Score](https://camo.githubusercontent.com/e39060cd9ee10283b6c95618f8895d96c50d08bf0d5a3270adb6025450e6a216/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7370617469652f6c61726176656c2d736c75676761626c652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/spatie/laravel-sluggable)[![StyleCI](https://camo.githubusercontent.com/63cf9ea39b289cd4bcdabffa5e546d9316202cfc44dc712b3cb8988b3414b7a5/68747470733a2f2f7374796c6563692e696f2f7265706f732f34383531323536312f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/48512561)[![Total Downloads](https://camo.githubusercontent.com/b963aec389efa24249664496a2e8217c17c337bf27953a1e5c88bb4e407bd22e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f646476616c75652f6c61726176656c2d736c75676761626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/oddvalue/laravel-sluggable)

This package provides a trait that will generate a unique slug when saving any Eloquent model.

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

echo $model->slug; // ouputs "activerecord-is-awesome"
```

The slugs are generated with Laravels `Str::slug` method, whereby spaces are converted to '-'.

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

[](#installation)

You can install the package via composer:

```
composer require spatie/laravel-sluggable
```

Usage
-----

[](#usage)

Your Eloquent models should use the `Oddvalue\Sluggable\HasSlug` trait and the `Oddvalue\Sluggable\SlugOptions` class.

The trait contains an abstract method `getSlugOptions()` that you must implement yourself.

Your models' migrations should have a field to save the generated slug to.

Here's an example of how to implement the trait:

```
