Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

A PHP 7 / Laravel package to create slugs

Spatie, the company where I work, recently released a Laravel package called laravel-sluggable. It automatically creates unique slugs when saving a model.

To install the package you just need to put the provided Spatie\Sluggable\HasSlug-trait on all models that have slugs. The trait contains an abstract method getSlugOptions that should be implemented:

use Spatie\Sluggable\Slug;

public function getSlugOptions() : SlugOptions
    return SlugOptions::create()

This is the result when saving an Eloquent model.

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

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

Take a look at the readme of the package on GitHub to learn all the options.

There was already a very capable package by Colin Viebrock to create slugs. We’ve been using that package for all our projects since we started using Laravel. Colin’s package does a lot, but we’re only using a fraction of the functionality it provides. We’ve created our own sluggable package because we wanted to use something lightweight.

We also like to play around with the newest stuff. All our new projects will be using PHP 7 so it made sense to create a package that requires it. When you browse the code you’ll see usages of scalar type hints, return types, anonymous classes and the null coalescing operator.

We’re fully aware that requiring PHP 7 will mean that a lot of people won’t be able to use the package. But gaining popularity isn’t our main goal. We primarily create these packages for our own projects and for people who, like us, want to push forward.

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band.
  • Binalf Kassa

    I tried to install it but this is what I got:


    Could not find package spatie/laravel-sluggable at any version for your minimum-stability (stable). Check the package spelling or your minimum-stability

    • Do you have PHP 7 installed on your system? If you continue to have problems, open up an issue on GitHub and we’ll take it from there.

      • Binalf Kassa

        You are right. I was trying to install it in my local machine with php 5.5. It works perfectly in the latest version of laravel homestead with php 7.

        Thanks a lot and keep up the good work.