Search code examples
phplaravellaravel-5laravel-5.1laravel-cashier

Laravel 5.1 and Cashier not interacting with $user as expected


When I register a new user, Cashier's methods work fine to register that user. For instance, I have this in my User model:

public static function registerUser()
{
$user = new User;
$stripeToken = Input::get('stripeToken');
$user->subscription('yearly')->create($stripeToken);
$user->save();
}

I also have a "resubscribe" method in my User model that uses similar Cashier methods:

public static function resubscribe(){
    $email = Input::get('email');
    $user = DB::table('users')->where('email', '=', $email)->first();
    if ($user->everSubscribed()) {
        $credentials = Input::only(['email', 'password']);
        $stripeToken = Input::get('stripeToken');
        $user->subscription('monthly')->resume($stripeToken);
        $user->save(); 
     }
}

and I get the following error: "Call to undefined method stdClass::everSubscribed()".

If I remove that condition, I get the following error: "FatalErrorException in User.php line 124: Call to undefined method stdClass::subscription()"

If I replace these lines of code:

$email = Input::get('email');
$user = DB::table('users')->where('email', '=', $email)->first();

with this:

$user = new User;

Then I get no errors, but of course, I don't want to create a new User, I want to resubscribe my old user. I had this working fine in Laravel 4.2 with nearly identical code. Thanks for any help!


Solution

  • You need to use Eloquent instead of the Query Builder:

    $user = User::where('email', '=', $email)->first();

    The query builder DB::table('users')->where('email', '=', $email)->first() returns a stdClass object rather than an instance of your User model.