2017 © Pedro Peláez
 

library laravel-sluggable

Generate slugs when saving Eloquent models

image

edofre/laravel-sluggable

Generate slugs when saving Eloquent models

  • Friday, March 24, 2017
  • by edofre
  • Repository
  • 1 Watchers
  • 1 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 64 Forks
  • 0 Open issues
  • 15 Versions
  • 0 % Grown

The README.md

Generate slugs when saving Eloquent models

Code Climate Latest Stable Version Latest Unstable Version License, (*1)

This package provides a trait that will generate a unique slug when saving any Eloquent model., (*2)

$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 '-'., (*3)

Installation

You can install the package via composer: ``` bash $ composer require edofre/laravel-sluggable, (*4)


## Usage Your Eloquent models should use the `Edofre\Sluggable\HasSlug` trait and the `Edofre\Sluggable\SlugOptions` class. The trait contains an abstract method `getSlugOptions()` that you must implement yourself. Here's an example of how to implement the trait: ```php <?php namespace App; use Edofre\Sluggable\HasSlug; use Edofre\Sluggable\SlugOptions; use Illuminate\Database\Eloquent\Model; class YourEloquentModel extends Model { use HasSlug; /** * Get the options for generating the slug. */ public function getSlugOptions() : SlugOptions { return SlugOptions::create() ->generateSlugsFrom('name') ->saveSlugsTo('url'); } }

Want to use multiple field as the basis for a slug? No problem!, (*5)

public function getSlugOptions() : SlugOptions
{
    return SlugOptions::create()
        ->generateSlugsFrom(['first_name', 'last_name'])
        ->saveSlugsTo('url');
}

You can also pass a callable to generateSlugsFrom., (*6)

By default the package will generate unique slugs by appending '-' and a number, to a slug that already exists., (*7)

You can disable this behaviour by calling allowDuplicateSlugs., (*8)

public function getSlugOptions() : SlugOptions
{
    return SlugOptions::create()
        ->generateSlugsFrom('name')
        ->saveSlugsTo('url')
        ->allowDuplicateSlugs();
}

You can also put a maximum size limit on the created slug:, (*9)

public function getSlugOptions() : SlugOptions
{
    return SlugOptions::create()
        ->generateSlugsFrom('name')
        ->saveSlugsTo('url')
        ->slugsShouldBeNoLongerThan(50);
}

The slug may be slightly longer than the value specified, due to the suffix which is added to make it unique., (*10)

You can also override the generated slug just by setting it to another value then the generated slug., (*11)

$model = EloquentModel:create(['name' => 'my name']); //url is now "my-name";
$model->url = 'my-custom-url';
$model-save();

$model->name = 'changed name';
$model->save(); //url stays "my name"

If you don't want to create the slug when the model is initially created you can set use the `doNotGenerateSlugsOnCreate() function., (*12)

public function getSlugOptions() : SlugOptions
{
    return SlugOptions::create()
        ->generateSlugsFrom('name')
        ->saveSlugsTo('url')
        ->doNotGenerateSlugsOnCreate();
}

Similarly, if you want to prevent the slug from being updated on model updates, call doNotGenerateSlugsOnUpdate(). This can be helpful for creating permalinks that don't change until you explicitly want it to., (*13)

public function getSlugOptions() : SlugOptions
{
    return SlugOptions::create()
        ->generateSlugsFrom('name')
        ->saveSlugsTo('url')
        ->doNotGenerateSlugsOnUpdate();
}

If you want to explicitly update the slug on the model you can call generateSlug() on your model at any time to make the slug according to your other options. Don't forget to save() the model to persist the update to your database., (*14)

Change log

Please see CHANGELOG for more information what has changed recently., (*15)

Testing

bash $ composer test, (*16)

Credits

This is a fork from https://github.com/spatie/laravel-sluggable, I just converted it to work with php 5.6

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website., (*17)

License

The MIT License (MIT). Please see License File for more information., (*18)

The Versions

24/03 2017

dev-master

9999999-dev https://github.com/edofre/laravel-sluggable

Generate slugs when saving Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Edo Freriks

spatie laravel-sluggable

24/03 2017

v1.5.2.x-dev

1.5.2.9999999-dev https://github.com/edofre/laravel-sluggable

Generate slugs when saving Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Edo Freriks

spatie laravel-sluggable

24/03 2017

V1.5.2

1.5.2.0 https://github.com/edofre/laravel-sluggable

Generate slugs when saving Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Edo Freriks

spatie laravel-sluggable

02/01 2017

v1.5.1.x-dev

1.5.1.9999999-dev https://github.com/edofre/laravel-sluggable

Generate slugs when saving Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Edo Freriks

spatie laravel-sluggable

02/01 2017

V1.5.1

1.5.1.0 https://github.com/edofre/laravel-sluggable

Generate slugs when saving Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Edo Freriks

spatie laravel-sluggable

02/01 2017

v1.5.0.x-dev

1.5.0.9999999-dev https://github.com/edofre/laravel-sluggable

Generate slugs when saving Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Edo Freriks

spatie laravel-sluggable

02/01 2017

V1.5.0

1.5.0.0 https://github.com/edofre/laravel-sluggable

Generate slugs when saving Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Edo Freriks

spatie laravel-sluggable

23/11 2016

V1.4.0

1.4.0.0 https://github.com/edofre/laravel-sluggable

Generate slugs when saving Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

by Edo Freriks

spatie laravel-sluggable

13/06 2016

1.2.0

1.2.0.0 https://github.com/spatie/laravel-sluggable

Generate slugs when saving Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-sluggable

24/01 2016

1.1.0

1.1.0.0 https://github.com/spatie/laravel-sluggable

Generate slugs when saving Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-sluggable

12/01 2016

1.0.2

1.0.2.0 https://github.com/spatie/laravel-sluggable

Generate slugs when saving Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-sluggable

27/12 2015

1.0.1

1.0.1.0 https://github.com/spatie/laravel-sluggable

Generate slugs when saving Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-sluggable

24/12 2015

1.0.0

1.0.0.0 https://github.com/spatie/laravel-sluggable

Generate slugs when saving Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-sluggable

24/12 2015

0.0.1

0.0.1.0 https://github.com/spatie/eloquent-sluggable

An opinionated package to generate slugs for Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie eloquent-sluggable