php - 使用 Laravel Cashier 和 Jenssegers MongoDB 在 Stripe 中更新订阅后,订阅不会在数据库中更新
问题描述
我无法在 MongoDB 数据库中更新我的新订阅。我使用 Laravel Cashier、Stripe 和 Jenssegers MongoDB。
在条带仪表板中,用户已成功添加为客户和订阅者。
这是错误: [23:24:17] LOG.error: 在 null {"userId":"4ec1b45d36623t2269477d0... 上调用成员函数 prepare()
这是错误所在的位置:
return true;
}
$statement = $this->getPdo()->prepare($query);
$this->bindValues($statement, $this->prepareBindings($bindings));
这是我的控制器:
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());
}
}
这是我的用户模型:
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',
];
}
这是我的计划模型: 命名空间应用程序;
use Jenssegers\Mongodb\Eloquent\Model;
/**
* @method static findOrFail($plan_id)
*/
class Plan extends Model
{
protected $fillable = [
'name',
'price',
'stripe_plan_id'
];
}
这是我的订阅迁移:
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');
}
}
请帮我找出这个问题的根源以及如何解决它。
解决方案
部分解决方案: 虽然我在下面遇到同样的错误
"Call to a member function prepare() on null {"userId":"4ec1b45d36623t2269477d0...".
为了让订阅在数据库中更新,我进入了 Cashier 的 Subscription.php 文件并替换了
use Illuminate\Database\Eloquent\Model;
和
use Jenssegers\Mongodb\Eloquent\Model;
这将修复数据库更新问题,但 Connection.php 文件中仍然存在一些奇怪的东西导致错误。
$statement = $this->getPdo()->prepare($query);
推荐阅读
- java - 使用 Gson-extras 反序列化具有瞬态变量的多态 JSON
- java - Spring Data JPA - 带有 IN、`=`、LIKE for List 的存储库
- rest - Shopify API:如何获取大于的 id
- python - 网页抓取图像:找不到“rel”选择器
- c# - 通过不同的 URL 缓存应用程序 ASP.NET MVC
- swift - 更新到 Xcode 11.3.1 - 故事板重构后导航栏和一半的视图消失
- java - 为什么并行化此实现的遗传算法的代码以解决 tsp 的速度要慢得多?
- ios - 在 PromiseKit 中获取第一个响应对象然后完成块
- mysql - 从远程连接到 Mysql 数据库
- sql - 有什么方法可以在 pyspark 数据框中找到包含数据的列数