Slug Model
The Slug model represents a URL slug entry associated with any Eloquent model via a polymorphic relation. Each model using HasUrl has exactly one slug row.
Namespace
JobMetric\Url\Models\Slug
Overview
The Slug model stores:
- Slug: The URL-friendly identifier (normalized, max 100 chars)
- Collection: Optional grouping for organizing slugs
- Polymorphic relation: Links to any model using
HasUrl
Properties
Slugable Type
public string $slugable_type;
Model class name (polymorphic).
Slugable ID
public int $slugable_id;
Model ID (polymorphic).
Slug
public string $slug;
URL-friendly slug (normalized, max 100 chars).
Collection
public string|null $collection;
Optional collection name for organizing slugs.
Table Structure
| Column | Type | Description |
|---|---|---|
id | int | Primary key |
slugable_type | string | Model class name (polymorphic) |
slugable_id | int | Model ID (polymorphic) |
slug | string | URL-friendly slug (max 100 chars) |
collection | string|null | Optional collection name |
deleted_at | timestamp|null | Soft delete timestamp |
created_at | timestamp | Creation timestamp |
updated_at | timestamp | Update timestamp |
Relationships
slugable (MorphTo)
Polymorphic relation to the model that owns this slug:
$slug = Slug::find(1);
$model = $slug->slugable; // Product, Category, etc.
Query Scopes
active()
Returns only active (non-deleted) slugs:
Slug::active()->get();
ofSlugable(string $type, int $id)
Filter by slugable type and ID:
Slug::ofSlugable(Product::class, 1)->first();
whereCollection(?string $collection)
Filter by collection (NULL-safe):
Slug::whereCollection('products')->get();
Slug::whereCollection(null)->get(); // NULL collection
Accessors
slugable_resource
Returns the resource representation of the slugable model (via UrlableResourceEvent):
$slug = Slug::find(1);
$resource = $slug->slugable_resource; // Resource from event listeners
Complete Examples
Finding Slugs
// Find by slugable
$slug = Slug::ofSlugable(Product::class, 1)->first();
// Find active slugs
$slugs = Slug::active()->get();
// Find by collection
$slugs = Slug::whereCollection('products')->get();
Accessing Related Model
$slug = Slug::find(1);
$product = $slug->slugable; // Product instance
Querying with Relationships
// Eager load related model
$slugs = Slug::with('slugable')->get();
// Filter by related model type
$slugs = Slug::where('slugable_type', Product::class)->get();
Related Documentation
- HasUrl Trait - Trait that manages slugs
- Url Model - Model that stores full URLs