PHPackages                             lightframes/laravel-slugmaker - 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. lightframes/laravel-slugmaker

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

lightframes/laravel-slugmaker
=============================

Generate slugs when saving Eloquent models in a separate table

v1.2.3(3y ago)012MITPHPPHP &gt;=7.4.33

Since Oct 30Pushed 3y ago1 watchersCompare

[ Source](https://github.com/lightframes/laravel-slugmaker)[ Packagist](https://packagist.org/packages/lightframes/laravel-slugmaker)[ Docs](https://github.com/lightframes/laravel-slugmaker)[ RSS](/packages/lightframes-laravel-slugmaker/feed)WikiDiscussions master Synced today

READMEChangelog (4)Dependencies (1)Versions (4)Used By (0)

Generate slugs in a separate table when saving Eloquent models
==============================================================

[](#generate-slugs-in-a-separate-table-when-saving-eloquent-models)

[![Latest Version on Packagist](https://camo.githubusercontent.com/ea3e091becb5dcea449fc381fa0e7800ac162adb02ba3339d0365285b6a210f1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c696768746672616d65732f6c61726176656c2d736c75676d616b65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lightframes/laravel-slugmaker)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

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

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 lightframes/laravel-slugmaker
```

---

! For Laravel &lt; v5.5

Add the ServiceProvider to the providers array in config/app.php:

```
Lightframes\SlugMaker\SlugMakerServiceProvider::class,
```

---

Publish config file:

```
php artisan vendor:publish --tag=slugmaker-config
```

Publish the migration file:

```
php artisan vendor:publish --tag=slugmaker-migrations
```

and run the migration:

```
php artisan migrate
```

Usage
-----

[](#usage)

### Configure

[](#configure)

! Pay attention! Your model should not have a field named "slug"

Your Eloquent models should use the `Lightframes\SlugMaker\ModelHasSlug` trait.

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

Also the trait contains public method `slug()` for relations your item-model with item-slug:

```
public function slug()
{
    return $this->morphOne(\Lightframes\SlugMaker\Models\Slug::class, 'slugable');
}
```

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

```
