I have created couple of custom tables that has the subscription_id (ID from cashier subscription table) foreign key. I would like to add the eloquent relation to my models and the same hasMany relation in Subscription model. How can I extend the subscription model from cashier please?
I had to add a custom relation to the subscription model. Not sure where exactly you want to use this, but in my case it always starts with the user object. I was able to override the subscriptions method in the user class like shown below:
public function subscriptions()
{
return $this
->hasMany(YourSubscription::class, $this->getForeignKey())
->orderBy('created_at', 'desc');
}
@Mr-Anonymous - you can simply add a new Subscription model in your project, and extend the Cashier Subscription Model File.
e.g. make the following file in your app directory (or where ever your model files are stored):
<?php
namespace App;
use Laravel\Cashier\Subscription as CashierSubscription;
class Subscription extends CashierSubscription
{
public function yourCustomMethod()
{
// your custom code here
}
}
You will then have all the functionality of the Laravel Subscription methods, plus any custom ones you need.
You also need to override the relationship on the User model.
/**
* Get all of the subscriptions for the Stripe model.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function subscriptions()
{
return $this->hasMany(\App\Subscription::class, $this->getForeignKey())->orderBy('created_at', 'desc');
}
Yeah you should be able to achieve this by only overwriting the subscriptions method on the billable model.
Superb. Perfect
Most helpful comment
@Mr-Anonymous - you can simply add a new Subscription model in your project, and extend the Cashier Subscription Model File.
e.g. make the following file in your app directory (or where ever your model files are stored):
You will then have all the functionality of the Laravel Subscription methods, plus any custom ones you need.