Cashier-stripe: Call to undefined method Illuminate\Database\Query\Builder::valid() using $user->subscribed('main')

Created on 18 Oct 2016  路  3Comments  路  Source: laravel/cashier-stripe

I get the following error when checking subscription status with:

$user = User::where('id', '=', \Auth::user()->id)->first();
if ($user->subscribed('main')) 

Error:

ErrorException in Builder.php line 2405: Call to undefined method
     Illuminate\Database\Query\Builder::valid() (View: /var/www/html...

 in Builder.php line 2405
at Builder->__call('valid', array())
at Builder->valid()
at call_user_func_array(array(object(Builder), 'valid'), array()) in Builder.php line 1426
at Builder->__call('valid', array())
at Builder->valid()
at call_user_func_array(array(object(Builder), 'valid'), array()) in Relation.php line 343
at Relation->__call('valid', array()) in Billable.php line 172
at HasMany->valid() in Billable.php line 172
at User->subscribed('main') in BillingsController.php line 58
at BillingsController->index()

I've created a new subscription using:

$user = User::find(1);
$user->newSubscription('main', 'monthly')->create($creditCardToken);

I can see the entry in my subscriptions table.

I don't get any problems using the $user->onGenericTrial() and $user->onTrial() methods when trial_ends_at contains a valid timestamp on User, but if trial_ends_at is null, I get a similar problem:

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

I'm using Laravel 5.2 and Cashier 6.0. I followed docs here https://laravel.com/docs/5.2/billing#checking-subscription-status . In my user model, I use LaravelCashier\Billable; and use Billable; and hasMany on my Subscription model. My Subscription model has the belongsTo User relation

Most helpful comment

Finally found the problem! One must NOT create relationships between User and Subscription models. I had the hasMany relationship on my User model and the belongsTo relationship on the Subscription model. That was the problem.

All 3 comments

Finally found the problem! One must NOT create relationships between User and Subscription models. I had the hasMany relationship on my User model and the belongsTo relationship on the Subscription model. That was the problem.

@cvcv01 You should close your own issue if you found the solution yourself.

Sorry to comment on an old and closed issue, but why can we not have a relationship between the two models? Shame the documentation doesn't warn against it either..

Was this page helpful?
0 / 5 - 0 ratings