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
HasUrltrait - Model does not implement
UrlContractinterface
When It's Thrown
The exception is thrown automatically when:
- A model uses
HasUrltrait - The model does not implement
UrlContractinterface - 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);
}
}
Related Documentation
- HasUrl Trait - Trait that requires UrlContract
- UrlContract - Interface that must be implemented