Overview
Lunar uses the Laravel-medialibrary package by Spatie to handle media across all models. Rather than reinvent the wheel, Lunar leverages this battle-tested package and adds its own conventions on top, including configurable media definitions, automatic image conversions, and primary image management.Supported Models
The following models support media out of the box via theLunar\Base\Traits\HasMedia trait:
| Model | Class | Has Thumbnail |
|---|---|---|
| Product | Lunar\Models\Product | Yes |
| Product Option | Lunar\Models\ProductOption | No |
| Product Option Value | Lunar\Models\ProductOptionValue | No |
| Collection | Lunar\Models\Collection | Yes |
| Brand | Lunar\Models\Brand | No |
| Asset | Lunar\Models\Asset | No |
Lunar\Models\ProductVariant does not use the HasMedia trait directly. Instead, it uses a many-to-many relationship with media through a pivot table. See Product Variant Images for details.Configuration
The media configuration is published atconfig/lunar/media.php.
| Key | Description |
|---|---|
definitions | Maps model aliases to their media definition class. Each model can have its own definition class to customize collections and conversions. |
collection | The default media collection name used across all models. |
fallback.url | A fallback URL returned when a model has no media. |
fallback.path | A fallback file path returned when a model has no media. |
definitions keys use kebab-case aliases derived from the model class name (e.g., Product becomes product, ProductOptionValue becomes product-option-value). You can also use the fully qualified class name as the key.
Adding Media
Adding media to a model follows the standard Spatie Media Library API.Retrieving Media
Primary Images
Lunar adds a concept of “primary” images on top of Spatie’s media library. Each model can have one image marked as primary per collection, tracked via aprimary custom property on the media item.
Thumbnail Relationship
Models using theHasMedia trait have a thumbnail relationship that returns the primary image:
Automatic Primary Management
Lunar includes aMediaObserver that automatically enforces the following rules:
- When a media item is marked as primary, all other items in the same collection are unmarked.
- When the primary image is deleted, the first remaining image in the collection is automatically promoted to primary.
- When a new image is added to an empty collection, it is automatically marked as primary.
Setting the Primary Image
Product Variant Images
ProductVariant handles media differently from other models. Instead of using the HasMedia trait, it uses a many-to-many relationship with the media table through a media_product_variant pivot table.
| Column | Type | Description |
|---|---|---|
primary | boolean | Whether this is the primary image for the variant |
position | smallInteger | Display order of the image |
Fallback Images
If a model has no media, callinggetFirstMediaUrl or getFirstMediaPath returns an empty string by default. Fallback images can be configured in config/lunar/media.php or via environment variables:
useFallbackUrl and useFallbackPath methods on the media collection.
Default Conversions
TheStandardMediaDefinitions class registers the following image conversions by default:
| Conversion | Width | Height | Notes |
|---|---|---|---|
small | 300 | 300 | Used by the admin panel for thumbnails. Registered globally (runs for all collections). |
medium | 500 | 500 | Registered on the images collection. |
large | 800 | 800 | Registered on the images collection. |
zoom | 500 | 500 | Registered on the images collection. |
Fit::Fill for sizing, a white background, and preserve the original image format.
Custom Media Definitions
To customize the media collections and conversions for a model, create a class that implementsLunar\Base\MediaDefinitionsInterface:
config/lunar/media.php:
Regenerating Conversions
After changing conversion definitions, regenerate existing conversions using the Spatie artisan command:Extending Your Own Models
Custom models can be given media support using the LunarHasMedia trait:
Definition Resolution
TheHasMedia trait resolves the media definition class for a model using the following priority:
- A snake_case alias in
config('lunar.media.definitions')(e.g.,productforProduct) - The fully qualified class name in the config
- The parent class name in the config (useful for extended models)
- Falls back to
Lunar\Base\StandardMediaDefinitions