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

xspark's avatar

Spark Plan Attributes

I am trying to implement limitations for users based on their subscribed plans.

Can I define attributes for each plan in SparkServiceProvider.php and use that for validation?

Code from my SparkServiceProvider.php

        Spark::plan('Pro', 'pro')
            ->price(19)
            ->features([
                '3 Posts'
            ])
            ->attributes([
                'max_posts' => 3
            ]);

How can I access user's current plan attributes, features and price in my controller?

Edit: I just figured out the solution

Spark::plans()->where('id', Auth::user()->current_billing_plan)->first();

Edit 2: Here is a simpler solution. It returns current user's plan object even if he has cancelled but still has access to plan benefits.

$user->sparkPlan();
0 likes
4 replies
EventFellows's avatar
Level 16

@xspark The quickest way that comes to mind would be get the current plan

$user->current_billing_plan;     // current plan is stored in the user or team table depending on your setup

And the simply get the Collection of all plans and retrieve the attributes you need from it (loop through it or whatever feels good to you):

Spark::plans()

Putting it together step-by-step this should do the trick:

$user = Auth::user();       // or any other way to get the user object
$current_plan_id = $user->current_billing_plan;     // note that the way it is defined in spark the ID is not an integer but the slug e.g. 'free', 'pro'

// Spark::plans() give you a collection of all plans you have defined in SparkServiceProvider
$current_plan = Spark::plans()->where('id', $current_plan_id)->first();

$current_plan->attributes['max_posts'];     // 3 if the current users plan is the PRO plan
  • freePlan technically is not a plan defined on the database so you might need to catch it with an if statement or similar if that is needed

Instead of Spark::plans() you can do many other things out of the box, just browse through /spark/src/Configuration/ManagesAvailablePlans.php for what is available.

2 likes
xspark's avatar

Thanks @EventFellows

This solution works.

If a user cancels the subscription and still have benefits of the plan for rest of the month, $user->current_billing_plan returns empty.

How can I get billing plan name for the user who has cancelled subscription but still have benefits of the plan?

Edit: I figured out the solution. It returns current user's plan object even if he has cancelled but still has access to plan benefits.

$user->sparkPlan();
EventFellows's avatar

True, this works.

On cancellation it gets removed from the user / team table but is still persisted on the 'subscriptions' table. So if you need to do some more magic around canceled subscriptions you can also access it directly from the subscriptions table.

DB::table('subscriptions')->where('user_id', $user->id)->first();

Please or to participate in this conversation.