Slug
Creates and manages URL-friendly slugs for database records
The withSlug
mixin adds slug generation and management capabilities to your models. Slugs are URL-friendly string identifiers, commonly used in content management systems and blogs to create readable, SEO-friendly URLs.
Usage
API Reference
withSlug()
Enhances a model with slug generation and management capabilities.
Basic Parameters (Simple Form)
model
- The base model to enhanceslugField
- Field to store the slug (e.g., 'slug')sourceField
- Source field to generate slug from (e.g., 'title')
Advanced Options (Configuration Form)
Added Methods
findBySlug()
Finds a record by its slug.
insertWithSlug()
Inserts a record with an automatically generated slug.
insertIfNotExistsWithSlug()
Inserts a record with a slug if it doesn't already exist based on a unique column.
upsertWithSlug()
Updates an existing record or inserts a new one, generating a slug if needed.
insertMany()
Inserts multiple records with automatically generated slugs.
generateUniqueSlug()
Generates a unique slug for the given values without inserting a record.
Features
- Automatic Slug Generation: Create URL-friendly slugs from title/name fields
- Customizable Formatting: Control separator, length, and word replacement
- Uniqueness Handling: Automatically adds numerical suffixes to ensure uniqueness
- Multiple Source Fields: Generate slugs from multiple fields (e.g., combining name and category)
- Batch Operations: Support for inserting multiple records with unique slugs