Reputation: 645
anyone used Laravel Cashier and has an answer to my below question. I want to cancel a subscription and in the docs it's written like that
$user->subscription('default')->cancelNow();
In my case the user owns a place and i added an extra table to the subscriptions migration which is "place_id" and has multiple subscriptions for each place he owns, can i make the above code more specific and write something like this so he can cancel the specific subscription of a place he owns
$user->subscription($planName)->where('place_id','=',$placeID)->cancelNow()
Also tried a subscription query
$subscription = Subscription::query()->active()->
where(['place_id', '=', $placeID], ['user_id', '=', $user->id])->get();
$subscription->cancelNow()
Doesn't work, it's just something i tried, anyone got any ideas? I'm just looking for a way to cancel a subscription with a more specific code, not just the name of the subscription. Thanks and regards!
Upvotes: 2
Views: 1718
Reputation: 645
So i solved my issue, instead of using
->get();
I used:
->first();
Like this:
$subscription = Subscription::query()->active()->where([['place_id', '=', $placeID], ['user_id', '=', $user->id]])->first();
$subscription->cancelNow();
That's because first() method will return only one record, while the get() method will return an array of records even though in my case it returned only one record as it was supposed to return, but first() made it work and it solved my problem.
Upvotes: 2
Reputation: 847
I have a similar situation where the paying user can have more than one subscriber with more than one subscription. I managed this by creating a migration that adds a subscriber_id
field to the subscriptions table
Schema::table('subscriptions', function (Blueprint $table) {
$table->integer('subscriber_id')->nullable()->after('user_id');
});
and then updating that ID after creating the subscription.
$subscription = $subscription->create($paymentMethod->id); // returns stripe subscription
$subscription = Subscription::find($subscription->id); // so you have ot get the local subscription from the db
$subscription->update(['subscriber_id' => $subscriber->id]);
I can then get all the subscriptions for a subscriber or a specific subscription by subscriber id and plan
$subscription = Subscription::where('subscriber_id', $subscriber->id)->where('stripe_plan','your_plan')...```
Essentially you could do the same but with place_id
Upvotes: 3