Overview
URLs provide SEO-friendly slugs for models such as products, collections, and brands. Instead of exposing database IDs in storefront routes:URLs are not to be confused with Laravel routes. They provide a slug-based lookup mechanism for storefront routing, not route definitions.
Model
Fields
Relationships
Scopes
Creating a URL
URLs can be created directly or through a model’surls() relationship.
Default URL behavior
Only one URL can be the default per element and language combination. When a new URL is created or updated withdefault set to true, any existing default URL for that same element and language is automatically set to false.
Deleting a URL
When a default URL is deleted, Lunar automatically promotes another URL for the same element and language to become the new default.Adding URL support to models
Lunar ships with URL support on the following models:Lunar\Models\ProductLunar\Models\CollectionLunar\Models\Brand
HasUrls trait:
Available relationships
TheHasUrls trait provides the following relationships:
When a model using the
HasUrls trait is deleted (hard delete only), all associated URLs are automatically removed.Automatic URL generation
Lunar can automatically generate a URL when a model with theHasUrls trait is created. This is controlled by the generator option in config/lunar/urls.php.
Default generator behavior
The built-inLunar\Generators\UrlGenerator performs the following steps when a model is created:
- Checks whether the model already has any URLs (skips generation if it does).
- Reads the model’s
namecolumn, falling back to thenameattribute (viaattr('name')). - Converts the name to a slug using
Str::slug(). - Ensures uniqueness by appending a numeric suffix if the slug already exists (e.g.
test-product,test-product-2,test-product-3). - Creates the URL as the default for the system’s default language.
Custom generator
To customize URL generation, create a class with ahandle method that accepts an Eloquent model:
null: