laravel-cashier

how to count subscriptions in laravel-cashier


I've got subscriptions working in Laravel-Cashier. Now I'd like to be able to see how many of my users have subscribed. The docs say to use Subscription::query()->active();, but this gives me an error - Class "Subscription" not found.

I don't have a Subscription class, but there is a subscription table used by Cashier. I could create a class based on that table, but I'm concerned that will mess up Cashier's own logic (surely Cashier has Subscription class I can't access). I'm also not convinced it will provide the correct information since it may count past subscriptions.

$user->subscribed('default') correctly returns the subscription status of an individual user, but User::all()->subscribed('default') returns an error BadMethodCallException Method Illuminate\Database\Eloquent\Collection::subscribed does not exist.

I tried creating my own method in User as follows:

public function paid()
    {
        return $this->subscribed('default');
    }

This has the same problem that I can use it on an individual user, but not on a collection.

I could loop through all my users and check individually, but that feels like overkill. Surely there's a way to simply publish Cashier's Subscription class so that I can run the command they suggest - Subscription::query()->active();

Or is there a way of running subscribed/paid on a collection that I'm missing?


Solution

  • I was able to access the Subscription model by adding

    use Laravel\Cashier\Subscription;
    

    to the controller file.