Fix a critical bug: refactors model bindings to use config-based resolution #118
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Replaces direct model class dependencies with configurable model bindings to improve flexibility and maintainability. Now, model classes can be swapped out via configuration without modifying the core code.
Key Changes
This improves package extensibility by allowing users to override default models through configuration rather than inheritance.
Use Case & Background
In certain scenarios—such as in a multi-tenant application—I needed to override the model’s default connection. Despite having already overridden the default models in the
soulbscription.php
config, unexpected exceptions occurred when some parts of the code still referenced the original model classes directly rather than the user-configured ones.For example, using
$subscriber->subscription->plan
or other relationships worked fine because those models were loaded correctly from the custom config. However, methods like$subscriber->getFeaturesAttribute()
triggered exceptions by referencingLucasDotVin\Soulbscription\Models\Feature
instead of the customApp\Models\Feature
. This caused queries against the wrong database connection, resulting in "table not found" errors in my tenant database.By replacing all direct references with configurable bindings, this PR ensures that every part of the package consistently respects the models defined in the
soulbscription.php
config, eliminating the need to modify core code for custom requirements.Screenshots & Illustrations
Example of Configured Model Override
Override the Default Connection
Showcase Example
Using
$subscriber->subscription->plan
or any relation works correctly, because the relation loads the models from the customsoulbscription.php
config:However, calling
$subscriber->getFeaturesAttribute()
triggers an exception that the features table is not found in the tenant database (which is correct, as it resides in the main app database). The root issue is thatgetFeaturesAttribute()
callsloadTicketFeatures()
, which referencesLucasDotVin\Soulbscription\Models\Feature
instead of the customApp\Models\Feature
.Consequently, these errors appear in any method that doesn’t pull the models from the
soulbscription.php
config.Conclusion
This PR ensures that all references to model classes within the package are resolved using the configurable bindings defined by the user. It significantly enhances maintainability and extensibility, allowing developers to override database connections or other model properties in a single location without modifying or forking the package’s internal code.
Thank you for reviewing!