Skip to main content

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

ColumnTypeDescription
idintPrimary key
slugable_typestringModel class name (polymorphic)
slugable_idintModel ID (polymorphic)
slugstringURL-friendly slug (max 100 chars)
collectionstring|nullOptional collection name
deleted_attimestamp|nullSoft delete timestamp
created_attimestampCreation timestamp
updated_attimestampUpdate 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();
$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();