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

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

padosoft/laravel-sluggable
==========================

Generate slugs when saving Eloquent models

3.8.0(1y ago)513.7k↓45%1[1 PRs](https://github.com/padosoft/laravel-sluggable/pulls)MITPHPPHP &gt;=7.0.0

Since Dec 24Pushed 9mo ago1 watchersCompare

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

READMEChangelog (3)Dependencies (5)Versions (23)Used By (0)

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

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

[![Latest Version on Packagist](https://camo.githubusercontent.com/80efea863baf05b0344d8672f1dd7055299a6e6397736657e05c12bca2c6496d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7061646f736f66742f6c61726176656c2d736c75676761626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/padosoft/laravel-sluggable)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/6c5ed8f9a5b7a8a8415f58861725ff59d8df8dbd2003b3004b695be4c89ee6ad/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7061646f736f66742f6c61726176656c2d736c75676761626c652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/padosoft/laravel-sluggable)[![Quality Score](https://camo.githubusercontent.com/1e2d67fe1dbd7f435bc727e8a13388bbaa9bec1d7ba31c7972846d06c6dc9504/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7061646f736f66742f6c61726176656c2d736c75676761626c652e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/padosoft/laravel-sluggable)[![Total Downloads](https://camo.githubusercontent.com/313feebf95ca469497b0ece655bf06f0138c0d0352e635c8bf4a57dec67cd21c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7061646f736f66742f6c61726176656c2d736c75676761626c652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/padosoft/laravel-sluggable)[![SensioLabsInsight](https://camo.githubusercontent.com/5ec45f1d9cef54dbf62f0907edf68d062b0611cea00a472b1c801e6c2098ee8c/68747470733a2f2f696d672e736869656c64732e696f2f73656e73696f6c6162732f692f32333639333063622d363163632d343333662d623836342d6535363630663435333365362e7376673f7374796c653d666c61742d737175617265)](https://insight.sensiolabs.com/projects/a56f8c11-331f-4d3c-8724-77f55969f2f7)

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

**NOTE:**This package is based on [spatie/laravel-sluggable](https://packagist.org/packages/spatie/laravel-sluggable)but with some adjustments for me and few improvements. Here's a major changes:

- Added the ability to specify a source field through a model relation with dot notation. Ex.: \['category.name'\] or \['customer.country.code'\] where category, customer and country are model relations.
- Added the ability to specify multiple fields with priority to look up the first non-empty source field. Ex.: In the example above, we set the look up to find a non empty source in model for slug in this order: title, first\_name and last\_name. Note: slug is set if at least one of these fields is not empty:

```
SlugOptions::create()->generateSlugsFrom([
						                'title',
						                ['first_name', 'last_name'],
							            ])
```

- Added option to set the behaviour when the source fields are all empty (thrown an exception or generate a random slug).
- Remove the abstract function getSlugOptions() and introduce the ability to set the trait with zero configuration with default options. The ability to define getSlugOptions() function in your model remained.
- Added option to set slug separator
- Some other adjustments and fix

\##Overview

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

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

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

\##Requires

- php: &gt;=7.0.0
- illuminate/database: ^5.0
- illuminate/support: ^5.0

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

[](#installation)

You can install the package via composer:

```
$ composer require padosoft/laravel-sluggable
```

Usage
-----

[](#usage)

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

The trait shipping with ZERO Configuration if your model contains the slug attribute and one of the fields specified in getSlugOptionsDefault(). If the zero config not for you, you can define `getSlugOptions()` method in your model.

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

```
