Skip to main content

ModelUrlContractNotFoundException

Thrown when a model uses HasUrl trait but does not implement UrlContract interface.

Namespace

JobMetric\Url\Exceptions\ModelUrlContractNotFoundException

Overview

This exception is thrown during model boot when:

  • Model uses HasUrl trait
  • Model does not implement UrlContract interface

When It's Thrown

The exception is thrown automatically when:

  1. A model uses HasUrl trait
  2. The model does not implement UrlContract interface
  3. The model is booted (during application startup or first use)

Example

// Bad: Missing UrlContract
class Product extends Model
{
use HasUrl; // Throws ModelUrlContractNotFoundException
}

// Good: Implements UrlContract
class Product extends Model implements UrlContract
{
use HasUrl;

public function getFullUrl(): string
{
return '/shop/' . ($this->slug ?? 'product-' . $this->id);
}
}

Error Message

The exception message is translated using url::base.exceptions.model_url_contract_not_found:

trans('url::base.exceptions.model_url_contract_not_found', [
'model' => $model
])

Handling

This exception should be caught during development to fix the model:

try {
$product = new Product();
} catch (ModelUrlContractNotFoundException $e) {
// Fix: Add UrlContract implementation
}

Prevention

Always implement UrlContract when using HasUrl:

class Product extends Model implements UrlContract
{
use HasUrl;

public function getFullUrl(): string
{
return '/shop/' . ($this->slug ?? 'product-' . $this->id);
}
}