如何在Laravel Cashier中计算订阅数?

huangapple go评论40阅读模式
英文:

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?

英文:

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?

答案1

得分: 0

我可以通过将以下代码添加到控制器文件来访问订阅模型:

use Laravel\Cashier\Subscription;
英文:

I was able to access the Subscription model by adding

use Laravel\Cashier\Subscription;

to the controller file.

huangapple
  • 本文由 发表于 2023年4月19日 23:44:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定