Skip to content

The SlugService Class

Shipu Ahamed edited this page Sep 3, 2016 · 1 revision

The SlugService Class

Unlike previous versions of the package, all the logic to generate slugs is handled by the \Cviebrock\EloquentSluggable\Services\SlugService class.

Generally, you won't need to access this class directly, although there is one static method that can be used to generate a slug for a given string without actually creating or saving an associated model.

use \Cviebrock\EloquentSluggable\Services\SlugService;

$slug = SlugService::createSlug(Post::class, 'slug', 'My First Post');

This would be useful for Ajax-y controllers or the like, where you want to show a user what the unique slug would be for a given test input, before actually creating a model. The first two arguments to the method are the model and slug field being tested, and the third argument is the source string to use for testing the slug.

You can also pass an optional array of configuration values as the fourth argument. These will take precedence over the normal configuration values for the slug field being tested. For example, if your model is configured to use unique slugs, but you want to generate the "base" version of a slug for some reason, you could do:

$slug = SlugService::createSlug(Post::class, 'slug', 'My First Post', ['unique' => false]);
Clone this wiki locally