This documentation is for an upcoming version and is subject to change.
namespace App; use ZaimeaLabs\Sluggable\HasSlug; use ZaimeaLabs\Sluggable\SlugOptions; use Illuminate\Database\Eloquent\Model; class EloquentModel extends Model { use HasSlug; /** * Create a new options for generating the slug. * * @return \ZaimeaLabs\Sluggable\SlugOptions */ public function newSlugOptions(): SlugOptions { return SlugOptions::create() ->generateSlugsFrom('name') ->saveSlugsTo('slug'); } }
/** * Get the route key for the model. */ public function getRouteKeyName(): string { return 'slug'; }
->generateSlugsFrom(['field', 'field_2']) ->saveSlugsTo('slug'); ->allowDuplicateSlugs(); ->slugsShouldBeNoLongerThan(50); ->usingSeparator('_'); ->doNotGenerateSlugsOnCreate(); ->doNotGenerateSlugsOnUpdate(); ->preventOverwrite(); ->startSlugSuffixFrom(2);
For convenience, you can use the alias findBySlug
to retrieve a model. The query will compare against the field passed to saveSlugsTo
when defining the SlugOptions
.
$model = Article::findBySlug('my-article');
findBySlug
also accepts a second parameter $columns
just like the default Eloquent find
method.