2

So I recently upgraded from Laravel 5.1 -> 5.4 and Cashier from 5.0 -> 7.0. In my blade I am using this check to see if a user is in their grace period

<?php if(Auth::check() && Auth::user()->onGracePeriod()): ?>

However now this code throws an exception

Call to undefined method Illuminate\Database\Query\Builder::onGracePeriod()

As per the documentation my user model has the import

use Laravel\Cashier\Billable;

and the use statement inside of the class itself

class User extends Model implements AuthenticatableContract, 

CanResetPasswordContract
{
     use Authenticatable, CanResetPassword, Billable;
    /**
     * The database table used by the model.
     *
     * @var string
...

Is there anything else that could cause this error? Searching through the code it looks like the function is within the Subscription.php within cashier but I cannot seem to find a fix. I also have the included dates that is often referenced in the documentation

protected $dates = ['trial_ends_at', 'subscription_ends_at'];

But I had that and my DB using that before back on 5.1 so I doubt that is related. Any ideas? The only thing I can think of is that when moving from 5.1 -> 5.4 I had to remove the "BillableContract" since it appears that it is no longer being used, is there something I have to replace that with? Thanks!

CMOS
  • 2,727
  • 8
  • 47
  • 83

1 Answers1

1

I believe you have to reference their subscription piece directly - not directly off the user (I think it's being used wrong is what I'm saying);

Per the documentation you check for onGracePeriod like this:

if ($user->subscription('main')->onGracePeriod()) {
    //
}
Hanny
  • 2,078
  • 6
  • 24
  • 52
  • Interesting, so the way I have my subscriptions setup is directly on the user ( I do not have any sort of pivot table that maps subscriptions to users ). My subscription table is just a definition of the subscriptions and then the user has the corresponding ID in their table. So historically I have been able to call things like onGracePeriod, do you have any idea when this changed? – CMOS Mar 08 '17 at 17:18
  • I'm not 100% sure - I recently started fiddling with Cashier myself and I noticed that virtually *all* the tutorials were out of date and their commands didn't work. I want to say that somewhere around v.6 or v.7 is when things definitely changed (including the DB structure!). It has made it pretty tricky for me having to find my way blindly, but I definitely stumbled with a similar issue as you are facing here because it's contrary to all the tutorials/vids/explanations out on the web! – Hanny Mar 08 '17 at 17:20
  • After further looking - it appears that between 5.1 and 5.2 is when that particular method (onGracePeriod) changed to be in it's current form. – Hanny Mar 08 '17 at 17:22
  • Perfect, thanks hanny. I will update my code and or my structure and get it sorted. – CMOS Mar 08 '17 at 17:23
  • Awesome. I believe this to be the accepted answer, so if you're feeling up to it, mark it as such so others might find this in the future if they're stumbling through it (like both you and I have now done!) – Hanny Mar 08 '17 at 17:33