0

I am having trouble getting my new subscription to be updated in MongoDB Database. Im using Laravel Cashier, Stripe, and Jenssegers MongoDB.

In the stripe dashboard, users have been successfully added as customers and subscribers.

Here is the ERROR: [23:24:17] LOG.error: Call to a member function prepare() on null {"userId":"4ec1b45d36623t2269477d0...

Here is where the ERROR lives:

                return true;
        }

        $statement = $this->getPdo()->prepare($query);

        $this->bindValues($statement, $this->prepareBindings($bindings));

Here is my controller:

namespace App\Http\Controllers;

use App\Plan;
use App\User;
use Exception;
use Illuminate\Http\Request;





class CheckoutController extends Controller
{
 /**
 * The collection name
 *
 * @var array
 */

public function checkout($plan_id)
{
    $plan = Plan::findOrFail($plan_id);
    $intent = auth()->user()->createSetupIntent();
    return view('billing.checkout', compact('plan', 'intent'));
}

public function process(Request $request)
{
    $plan = Plan::findOrFail($request->input('billing_plan_id'));
    try {
        auth()->user()->newSubscription($plan->name, $plan->stripe_plan_id)- 
>create($request->input('payment-method'));
        return redirect()->route('billing')->withMessage('Subscribed Successfully');
    } catch (Exception $e) {
        return redirect()->back()->withError($e->getMessage());
    }
}

Here is My User Model:

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Auth\User as Authenticatable;
use Laravel\Cashier\Billable;
use Illuminate\Foundation\Auth;

class User extends Authenticatable
{

use  Billable, Notifiable;


protected $connection = 'mongodb';


/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
 protected $fillable = [
    'name', 'email', 'username', 'password', 'phone', 'last_login_at', 
'last_login_ip',
];
/**
 * The collection name
 *
 * @var array
 */
 protected $table = 'users';



 /**
  * The attributes that are mass assignable.
  *
  * @var array
  */
   protected $dates = ['deleted_at'];
 /**
  * The attributes that should be hidden for arrays.
  *
  * @var array
  */
 protected $hidden = [
    'password', 'remember_token',
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

}

Here is My Plan Model: namespace App;

use Jenssegers\Mongodb\Eloquent\Model;


/**
 * @method static findOrFail($plan_id)
 */
class Plan extends Model
{

 protected $fillable = [
     'name',
    'price',
    'stripe_plan_id'
];

}

Here is my Subscription Migration:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;
use Jenssegers\Mongodb\Schema\Blueprint;





class CreateSubscriptionsTable extends Migration
{
/**
 * The name of the database connection to use.
 *
 * @var string
 */
protected $connection = 'mongodb';



public function up()
{
    Schema::create('subscriptions', function (Blueprint $collection) {
        $collection->bigIncrements('id');
        $collection->unsignedBigInteger('userid');
        $collection->string('name');
        $collection->string('stripe_id');
        $collection->string('stripe_status');
        $collection->string('stripe_plan')->nullable()->change();
        $collection->integer('quantity')->nullable()->change();
        $collection->timestamp('trial_ends_at')->nullable();
        $collection->timestamp('ends_at')->nullable();
        $collection->timestamps();
        $collection->index(['user_id', 'stripe_status']);
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('subscriptions');
}
}

Please help me figure out the source of this issue and how to solve it.

floatingLomas
  • 8,553
  • 2
  • 21
  • 27
Laing
  • 3
  • 5

2 Answers2

0

Make sure your model (i'm guessing it's a model but you didn't mention / show it) that is calling $this->getPdo() is extending Jenssegers eloquent model class and not Laravels.

For example:

Replace

class MyModel extends Model

With

class MyModel extends \Jenssegers\Mongodb\Eloquent\Model
Giovanni S
  • 2,050
  • 18
  • 33
  • I added my Models and Migrations to this post. After adding \Jenssegers\Mongodb\Eloquent\Model to both User and Plan Model, I still get the error. – Laing May 18 '20 at 01:20
  • where is ```$statement = $this->getPdo()->prepare($query);``` coming from? is that inside the Jessengers package or in your code? – Giovanni S May 18 '20 at 01:25
  • It is coming from …/vendor/laravel/framework/src/Illuminate/Database/Connection.php 457 – Laing May 18 '20 at 01:43
  • I dont see where to add /Jenssegers\Mongodb\Eloquent\Model in Connection.php where I see the $statement = $this->getPdo()->prepare($query); – Laing May 18 '20 at 13:32
0

Partial Solution: Although I am getting the same error below

"Call to a member function prepare() on null {"userId":"4ec1b45d36623t2269477d0...".

In order to get the Subscription to update in the database I went into the Subscription.php file in Cashier and I replaced

use Illuminate\Database\Eloquent\Model; 

with

use Jenssegers\Mongodb\Eloquent\Model;

This will fix database update issue, but something wierd is still going on in the Connection.php file causing the error.

 $statement = $this->getPdo()->prepare($query);
Laing
  • 3
  • 5