Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with Feature Usage validation #20

Open
stanwarri opened this issue Sep 4, 2024 · 0 comments
Open

Error with Feature Usage validation #20

stanwarri opened this issue Sep 4, 2024 · 0 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@stanwarri
Copy link

stanwarri commented Sep 4, 2024

The canUseFeature method is returning the first record in the database instead of filtering based on the slug. This is because the query is being applied to a fresh instance of the model that hasn't been properly utilized to build the query before first() is called in the function below in the SubscriptionUsage.php class

public function scopeByFeatureSlug(Builder $builder, string $featureSlug): Builder
    {
        $model = config('laravel-subscriptions.models.feature', Feature::class);
        $feature = tap(new $model())->where('slug', $featureSlug)->first();

        return $builder->where('feature_id', $feature ? $feature->getKey() : null);
    }

The tap(new $model()) simply returns the model instance itself. The where('slug', $featureSlug) method is then called, but since first() is immediately called on this query, it ends up executing the query before any further conditions are applied.

Instead of using tap(), you should directly chain the query methods on the model instance:

$feature = $model::where('slug', $featureSlug)->first();

@mckenziearts mckenziearts self-assigned this Nov 1, 2024
@mckenziearts mckenziearts added bug Something isn't working enhancement New feature or request labels Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants