Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Stelikas's avatar

Laravel Cashier, canceling specific subscription

Hello dear developers, so i run into a problem which the docs don't really explain much. I got a user which got a 1 year subscription for each place he owns, persay i want to cancel the subscription of the first place he owns, how do i cancel this specific subscription? I can't find a way.

I would think of something like this: (but they won't work)

$subscription = Subscription::query()->active()->where([['place_id', '=', $placeID], ['user_id', '=', $user->id]])->get();

    $subscription->cancelNow();

OR

$user->subscriptions()->active()->where('place_id', '=', $placeID)->cancelNow()

OR

$user->subscription($planName)->where('place_id', '=', $placeID)->cancelNow() 

Honestly tried many things but i'm not that experienced, anyone got a solution?

Thanks and regards!

0 likes
3 replies
J0wZ's avatar

Hello,

To cancel a specific subscription you need to give it a specific name, or at least some metadata I would say. Do you have implemented the place_id in your subscription table ? Which model is containing the Billable trait ? Place ? Or User ?

Stelikas's avatar

Hello, thanks for the reply.

Yes i implemented the place_id in my subscription table, the relationship works fine, User is containing the billable trait, i can cancel subscriptions just fine but i can't find a way to cancel a user's specific subscription.

This query works just fine, it returns the subscription of the place the user owns.

$subscription = Subscription::query()->active()->where([['place_id', '=', $placeID], ['user_id', '=', $user->id]])->get();

Although next

$subscription->cancelNow(); 

doesn't work.

It works like this as the documenation say

$user->subscription($planName)->cancelNow();

But what if the user has two same subscriptions (1 year for example) for two different places, i am trying to find a way with Laravel Cashier to cancel user's specific subscription.

Is there anything i can pass to

subscription()

like the $planName, so it can find and cancel the specific subscription i am looking for?

Stelikas's avatar
Stelikas
OP
Best Answer
Level 1

So i solved my issue, instead of using

->get(); 

I used:

->first();

Like this:

$subscription = Subscription::query()->active()->where([['places_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.

1 like

Please or to participate in this conversation.