Skip to main content

SlugNotFoundException

Thrown when attempting to find a model by slug but no matching slug exists.

Namespace

JobMetric\Url\Exceptions\SlugNotFoundException

Overview

This exception is thrown by:

  • findBySlugOrFail() - When slug doesn't exist
  • findBySlugAndCollectionOrFail() - When slug doesn't exist in collection

When It's Thrown

The exception is thrown when:

  1. findBySlugOrFail() is called with a slug that doesn't exist
  2. findBySlugAndCollectionOrFail() is called with a slug/collection that doesn't exist

Example

// Slug doesn't exist
try {
$product = Product::findBySlugOrFail('non-existent-slug'); // Throws SlugNotFoundException
} catch (SlugNotFoundException $e) {
// Handle not found
}

Handling

try {
$product = Product::findBySlugOrFail($slug);
} catch (SlugNotFoundException $e) {
abort(404, 'Product not found');
}

Error Message

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

trans('url::base.exceptions.slug_not_found')

Prevention

Use findBySlug() instead of findBySlugOrFail() if you want to handle null:

$product = Product::findBySlug($slug);
if (!$product) {
abort(404, 'Product not found');
}