英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论