Skip to content

Commit

Permalink
Merge pull request #32 from mnkincir/main
Browse files Browse the repository at this point in the history
Fixed duplicate feature slugs.
  • Loading branch information
mckenziearts authored Jan 7, 2025
2 parents fc9a378 + 8648089 commit 542dfe7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/Models/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public function recordFeatureUsage(string $featureSlug, int $uses = 1, bool $inc

public function reduceFeatureUsage(string $featureSlug, int $uses = 1): ?SubscriptionUsage
{
$usage = $this->usage()->byFeatureSlug($featureSlug)->first();
$usage = $this->usage()->byFeatureSlug($featureSlug, $this->plan_id)->first();

if ($usage === null) {
return null;
Expand All @@ -357,7 +357,7 @@ public function reduceFeatureUsage(string $featureSlug, int $uses = 1): ?Subscri
public function canUseFeature(string $featureSlug): bool
{
$featureValue = $this->getFeatureValue($featureSlug);
$usage = $this->usage()->byFeatureSlug($featureSlug)->first();
$usage = $this->usage()->byFeatureSlug($featureSlug, $this->plan_id)->first();

if ($featureValue === 'true') {
return true;
Expand All @@ -378,7 +378,7 @@ public function canUseFeature(string $featureSlug): bool
*/
public function getFeatureUsage(string $featureSlug): int
{
$usage = $this->usage()->byFeatureSlug($featureSlug)->first();
$usage = $this->usage()->byFeatureSlug($featureSlug, $this->plan_id)->first();

return (! $usage || $usage->expired()) ? 0 : $usage->used;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Models/SubscriptionUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public function subscription(): BelongsTo
return $this->belongsTo(config('laravel-subscriptions.models.subscription'), 'subscription_id', 'id', 'subscription');
}

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

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

0 comments on commit 542dfe7

Please sign in to comment.