2017 © Pedro Peláez
 

library sluggable

Automatically create slugs for your Eloquent models by hooking into the creating event

image

corazzi/sluggable

Automatically create slugs for your Eloquent models by hooking into the creating event

  • Friday, March 10, 2017
  • by corazzi
  • Repository
  • 1 Watchers
  • 1 Stars
  • 356 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 11 % Grown

The README.md

Sluggable

Automatically create slugs for your Eloquent models by hooking into the creating event, (*1)

Installation

Simply run composer require Corazzi/sluggable in your Laravel project., (*2)

Usage

To add automatic slug generation to a model, use the Corazzi\Sluggable\Sluggable trait., (*3)

namespace Acme\Models;

use Illuminate\Database\Eloquent\Model;
use Corazzi\Sluggable\Sluggable;

class Post extends Model
{
    use Sluggable;
}

Now when you create a new model that uses the trait, it will look for a name column and generate a new slug, which it will save in the slug column., (*4)

$post = Post::create([
    'name' => 'My first post'
]);

$post->slug; // my-first-post

Custom columns

If you would like to use different column names for your origin string and the slug, you can set the protected attributes $slugOrigin and $slugColumn on your model., (*5)

namespace Acme\Models;

use Illuminate\Database\Eloquent\Model;
use Corazzi\Sluggable\Sluggable;

class Post extends Model
{
    use Sluggable;

    protected $slugOrigin = 'title';

    protected $slugColumn = 'post_name';
}

Example

$post = Post::create([
    'title' => 'My second post'
]);

$post->post_name; // my-second-post

Existing slugs

If a slug exists for the model, it will automatically append an integer to the end of the slug until a unique one is generated., (*6)

// my-second-post already exists

$post = Post::create([
    'title' => 'My second post'
]);

$post->post_name; // my-second-post-1

$post = Post::create([
    'title' => 'My second post'
]);

$post->post_name; // my-second-post-2

// ...and so on

Explicitly setting slugs

You can explicitly set a slug when creating a new model and the automatic generation will be skipped, (*7)

$post = Post::create([
    'name' => 'How to explicitly set a slug',
    'slug' => 'explicitly-setting-slugs-with-sluggable'
]);

$post->slug; // explicitly-setting-slugs-with-sluggable

Empty origin

If the origin column is empty, a Corazzi\Sluggable\EmptyOriginException will be thrown., (*8)

The Versions

10/03 2017

dev-master

9999999-dev

Automatically create slugs for your Eloquent models by hooking into the creating event

  Sources   Download

MIT

by Sacha Corazzi

10/03 2017

v1.1.0

1.1.0.0

Automatically create slugs for your Eloquent models by hooking into the creating event

  Sources   Download

MIT

by Sacha Corazzi

07/02 2017

v1.0.0

1.0.0.0

Automatically create slugs for your Eloquent models by hooking into the creating event

  Sources   Download

MIT

by Sacha Corazzi