简体   繁体   中英

Laravel cashier : how to get plan name or id?

I have 8 stripe plans. 4 monthly + 4 yearly. like

plan_one.monthly
plan_one.yearly
plan_two.monthly
plan_two.yearly
plan_three.monthly
plan_three.yearly
plan_four.monthly
plan_four.yearly

and here is user subscription row

id -> 1
user_id -> 1
name -> Plan-One Monthly
stripe_id -> sub_xxxxxxxxxx
stripe_plan -> plan_one.monthly
quantity -> 1
trial_ends_at - null
ends_at - null
created_at - datetime()
updated_at - datetime()

In this situation how can i get user active/current subscription plan name?

The subscription method on Billable trait setup to return the last row. So if one billable entity only has one active subscription on your app, this works just fine:

$user->subscription('default')->stripe_plan;

tested on laravel 5.6 / cashier 7.1

I'm late to the party but I was looking for a similar solution, in my case I only allow the user to have one subscription at a time but I wanted to programmatically fetch the users active subscription without looping through the available subscriptions, reading the laravel docs more throughly I found what I needed using subscription scopes.

$user->subscriptions()->active()->first();

https://laravel.com/docs/8.x/billing#subscription-scopes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM