Skip to main content

Url Model

The Url model represents a versioned full URL entry associated with any Eloquent model via a polymorphic relation. It tracks the complete history of URLs for SEO-friendly redirects.

Namespace​

JobMetric\Url\Models\Url

Overview​

The Url model stores:

  • Full URL: The complete canonical URL path
  • Version: Version number for tracking history
  • Collection: Optional grouping (inherited from slug)
  • Polymorphic relation: Links to any model using HasUrl

Properties​

Urlable Type​

public string $urlable_type;

Model class name (polymorphic).

Urlable ID​

public int $urlable_id;

Model ID (polymorphic).

Full URL​

public string $full_url;

Complete canonical URL (max 2000 chars).

Collection​

public string|null $collection;

Optional collection name (inherited from slug).

Version​

public int $version;

Version number (starts at 1, increments on URL changes).

Table Structure​

ColumnTypeDescription
idintPrimary key
urlable_typestringModel class name (polymorphic)
urlable_idintModel ID (polymorphic)
full_urlstringComplete canonical URL (max 2000 chars)
collectionstring|nullOptional collection name
versionintVersion number (starts at 1)
deleted_attimestamp|nullSoft delete timestamp
created_attimestampCreation timestamp
updated_attimestampUpdate timestamp

Relationships​

urlable (MorphTo)​

Polymorphic relation to the model that owns this URL:

$url = Url::find(1);
$model = $url->urlable; // Product, Category, etc.

Query Scopes​

active()​

Returns only active (non-deleted) URLs:

Url::active()->get();

ofUrlable(string $type, int $id)​

Filter by urlable type and ID:

Url::ofUrlable(Product::class, 1)->get();

whereCollection(?string $collection)​

Filter by collection (NULL-safe):

Url::whereCollection('products')->get();

Accessors​

urlable_resource​

Returns the resource representation of the urlable model (via UrlableResourceEvent):

$url = Url::find(1);
$resource = $url->urlable_resource; // Resource from event listeners

Static Methods​

resolveActiveByFullUrl(string $fullUrl)​

Resolve the active model that currently owns a given full URL:

$model = Url::resolveActiveByFullUrl('/shop/laptops/macbook-pro-14');
// Returns Product instance or null

Parameters:

  • $fullUrl (string): The full URL to resolve

Returns: Model|null - The model instance or null if not found

resolveRedirectTarget(string $fullUrl)​

Resolve redirect target for a legacy URL:

$target = Url::resolveRedirectTarget('/shop/old-path');
// Returns current active URL or null

Parameters:

  • $fullUrl (string): The legacy URL to resolve

Returns: string|null - The current active URL or null if not found

Complete Examples​

Finding URLs​

// Find by urlable
$urls = Url::ofUrlable(Product::class, 1)->get();

// Find active URLs
$active = Url::active()->get();

// Find by collection
$urls = Url::whereCollection('products')->get();
$url = Url::find(1);
$product = $url->urlable; // Product instance

Resolving Active Model​

$model = Url::resolveActiveByFullUrl('/shop/laptops/macbook-pro-14');
if ($model instanceof Product) {
// Handle product
}

Resolving Redirects​

$target = Url::resolveRedirectTarget('/shop/old-path');
if ($target) {
return redirect($target, 301);
}

URL History​

$product = Product::find(1);
$history = $product->urlHistory();

foreach ($history as $url) {
echo $url->full_url . ' (version ' . $url->version . ')' . PHP_EOL;
}