Overview
Lunar provides a number of Eloquent models and quite often in custom applications additional relationships and functionality are needed.Dynamic Eloquent Relationships
The recommended way to extend Lunar’s models is by using Laravel’s dynamic relationships. This allows custom relationships to be added to any Lunar model without needing to replace it.boot method of a service provider. See the Laravel documentation on dynamic relationships for more information.
Replaceable Models
For cases where dynamic relationships are not sufficient, all Lunar models are replaceable. This means Lunar can be instructed to use a custom model throughout the ecosystem using dependency injection.Most use cases are better served by dynamic relationships above. Model replacement should only be used when deeper customization is required, such as overriding methods or adding scopes.
Registration
Custom models should be registered within theboot method of a service provider.
When registering models, the Lunar model’s contract should be set as the first argument and the custom model implementation as the second.
Registering multiple Lunar models.
If multiple models need to be replaced, instead of replacing them one by one, a directory can be specified for Lunar to scan for models. This assumes that each model extends its counterpart model, e.g.App\Models\Product extends Lunar\Models\Product.
Route binding
Route binding is supported and simply requires the relevant contract class to be injected.Relationship support
If a model used in a relationship is replaced, the custom model will be returned via relationship methods. For example, assuming a customApp\Models\ProductVariant has been registered:
Static call forwarding
If a custom model has additional methods, those functions can be called directly from the Lunar model instance. For example, to provide a new function on a product variant model:Observers
Observers that callobserve on a Lunar model will still work as intended when the model is replaced. Custom observers can reference the Lunar model and everything will be forwarded to the appropriate class.