Cashier-stripe: How to extend Subscription model?

Created on 2 Jan 2017  路  5Comments  路  Source: laravel/cashier-stripe

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?

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):

<?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.

All 5 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

4UForever picture 4UForever  路  3Comments

AnalyzeMe picture AnalyzeMe  路  5Comments

alexbowers picture alexbowers  路  6Comments

peeyush1234 picture peeyush1234  路  4Comments

cvcv01 picture cvcv01  路  3Comments