laravelcm / laravel-subscriptions

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

Can't get user subscription on planSubscription('main') function #23

Open syntx1997 opened 1 month ago

syntx1997 commented 1 month ago

Hello guys, I am really having trouble getting the user subscription, it always returns null even though the user was subscribed already, I have checked the sdubscriptions table on database and the user's id was there. It only happens on live server, no issue on local.

syntx1997 commented 1 month ago

Sorry, I just want to give some updates about my investigation about this. I think it is something related to this: $user->planSubscription('main')->getFeatureRemainings('listings'); the value main is actually from the slug field from the subscriptions table.

Subscriptions table:

Screenshot 2024-10-09 at 10 28 47 PM

I have checked the planSubscription function inside the HasPlanSubscriptions trait and this is what I found. I think this is the main reason for this. It's just happen that I only tested one subscription on my local (which means there's only one subscription data there with main slug) thats why this bug didn't show up. Screenshot 2024-10-09 at 10 30 50 PM

syntx1997 commented 1 month ago

I solve it by overriding the function by recreating a function on my User model where the trait has been applied.

public function planSubscription(string $subscriptionSlug): ?Subscription { return $this->planSubscriptions()->where('slug', 'like', '%' . $subscriptionSlug . '%')->first(); }

Redleks commented 2 weeks ago

Hi! I faced the same problem. It was an absolutely strange solution with unique slugs. I had to use my own class for features. I overwrite getSlugOptions function

public function getSlugOptions(): SlugOptions
    {
        return SlugOptions::create()
            ->doNotGenerateSlugsOnUpdate()
            ->generateSlugsFrom('name')
            ->saveSlugsTo('slug')
            ->extraScope(fn($builder) => $builder->where('plan_id', $this->plan_id));
    }

And remove unique key for slug in DB. My solution allows you to make a unique slug for a separate plan.