Skip to main content
Lunar uses Spatie’s Media Library package to manage images and files across models.

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 the Lunar\Base\Traits\HasMedia trait:
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 at config/lunar/media.php.
The 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.
For more information, see Associating files in the Spatie documentation.

Retrieving Media

For more information, see Retrieving media in the Spatie documentation.

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 a primary custom property on the media item.

Thumbnail Relationship

Models using the HasMedia trait have a thumbnail relationship that returns the primary image:

Automatic Primary Management

Lunar includes a MediaObserver 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.
The pivot table includes:

Fallback Images

If a model has no media, calling getFirstMediaUrl or getFirstMediaPath returns an empty string by default. Fallback images can be configured in config/lunar/media.php or via environment variables:
These values are passed to Spatie’s useFallbackUrl and useFallbackPath methods on the media collection.

Default Conversions

The StandardMediaDefinitions class registers the following image conversions by default: All conversions use 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 implements Lunar\Base\MediaDefinitionsInterface:
Then register the class in config/lunar/media.php:

Regenerating Conversions

After changing conversion definitions, regenerate existing conversions using the Spatie artisan command:
This creates queue jobs for each media entry to be reprocessed. See the Spatie documentation for more options.

Extending Your Own Models

Custom models can be given media support using the Lunar HasMedia trait:
This provides access to all Lunar media features including configurable definitions and automatic conversions.
To use Lunar’s media definitions and conversions, models must use the Lunar\Base\Traits\HasMedia trait. Using Spatie’s InteractsWithMedia trait directly bypasses Lunar’s media definition system. See the Spatie documentation for using the library directly.

Definition Resolution

The HasMedia trait resolves the media definition class for a model using the following priority:
  1. A snake_case alias in config('lunar.media.definitions') (e.g., product for Product)
  2. The fully qualified class name in the config
  3. The parent class name in the config (useful for extended models)
  4. Falls back to Lunar\Base\StandardMediaDefinitions