laravelcm / laravel-subscriptions

Laravel Subscriptions is a flexible plans and subscription management system for Laravel.
MIT License
111 stars 19 forks source link

Error with Feature Usage validation #20

Open stanwarri opened 2 months ago

stanwarri commented 2 months ago

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();