Skip to content

HasSlug ​

Namespace: Unusualify\Modularous\Entities\Traits\HasSlug

Generates and stores URL slugs in a dedicated {Model}Slug model. Supports multi-locale slugs, UTF-8 transliteration, and automatic slug suffixing to avoid collisions. Overrides resolveRouteBinding() to look up models by slug rather than primary key.


Boot Behavior ​

EventAction
savedCalls setSlugs() to generate/update slug records
restoredCalls setSlugs($restoring = true) to re-activate the slug

Relationship ​

php
public function slugs(): HasMany   // → {Model}Slug model, one record per locale

Methods ​

MethodSignatureDescription
setSlugs(bool $restoring = false): voidGenerates/updates slug records for all locales from $slugAttributes
getSlug(?string $locale = null): stringReturns the active slug for the locale (falls back to fallback_locale when configured)
getActiveSlug(?string $locale = null): ?objectReturns the active Slug record for the locale
getFallbackActiveSlug(): ?objectReturns the active slug in the fallback locale
getSlugsTable(): stringReturns the database table name for this model's slugs
getForeignKey(): stringReturns the foreign key column name used in the slugs table
resolveRouteBinding(mixed $value, ?string $field = null): staticResolves the model from a slug value (published + visible scope applied)
getUtf8Slug(string $str, array $options = []): stringConverts a UTF-8 string to a URL-safe slug using a built-in character map
disableLocaleSlugs(string $locale, int $exceptId = 0): voidDeactivates all slugs for a locale except the given ID

Scopes ​

ScopeDescription
scopeExistsSlug($query, $slug)Models with an active slug matching the value in the current locale
scopeExistsInactiveSlug($query, $slug)Models with any (active or inactive) matching slug
scopeExistsFallbackLocaleSlug($query, $slug)Models with an active slug in the fallback locale

Computed Attributes ​

AttributeDescription
slugVirtual — returns getSlug() for the current locale

Configuration ​

PropertyTypeDescription
$slugAttributesarrayFields used to build the slug (e.g. ['title']). First element is the slug source, additional elements are dependency fields
$slugModelClassstring|nullOverride the auto-resolved {Model}Slug class
$slugForeignKeystring|nullOverride the foreign key column name in the slugs table
php
// In your model
protected $slugAttributes = ['title'];
// Or with a dependency (e.g., scoped to a category)
protected $slugAttributes = ['title', 'category_id'];

Usage ​

php
use Unusualify\Modularous\Entities\Traits\HasSlug;

class Post extends Model
{
    use HasSlug;

    protected $slugAttributes = ['title'];
}

// Reading slugs
$post->slug;              // current locale
$post->getSlug();         // same
$post->getSlug('fr');     // French slug

// Checking existence
Post::existsSlug('my-post')->first();

// Route model binding (automatic via resolveRouteBinding)
// Route::get('/posts/{post}', ...) → looks up by slug