Laravel 自定义订阅更新 subscribed_at 所有更新
Laravel customized subscription updating subscribed_at for all the update
我使用的是自定义订阅,它不是 laravel 的默认收银台。
迁移如下所示
public function up()
{
Schema::create('subscriptions', function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->bigInteger('plan_id')->unsigned();
$table->foreign('plan_id')->references('id')->on('subscription_plans');
$table->bigInteger('transaction_id')->unsigned();
$table->foreign('transaction_id')->references('id')->on('transactions');
$table->timestamp('subscribed_at');
$table->timestamp('expires_at')->nullable();
$table->boolean('is_active')->default(true);
$table->json('benefits');
$table->timestamps();
});
}
用户关系如下
// subscription
public function subscriptions(): HasMany
{
return $this->hasMany(Subscription::class, 'user_id')->orderBy('subscribed_at', 'asc');
}
在特定情况下,我更新了 is_active
标志。
// in user model : User.php
public function createSubscription(Plan $plan): Subscription
{
$transaction = $this->createTransaction($plan);
$data = [
'plan_id' => $plan->id,
'transaction_id' => $transaction->id,
'is_active' => true,
'subscribed_at' => now(),
'expires_at' => now()->addDays($plan->validity),
'is_active' => true,
'benefits' => $plan->benefits
];
$this->subscriptions()->update(['is_active' => false]);
return $this->subscriptions()->create($data);
}
但它正在更新所有 subscribed_at
时间戳。
请帮我解决这个问题。
将 subscribed_at 列类型从 timestamp 更改为 dateTime(或者让它为 nullable())
喜欢:
Schema::create('subscriptions', function (Blueprint $table) {
// ...
$table->dateTime('subscribed_at');
// ...
});
我使用的是自定义订阅,它不是 laravel 的默认收银台。 迁移如下所示
public function up()
{
Schema::create('subscriptions', function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->bigInteger('plan_id')->unsigned();
$table->foreign('plan_id')->references('id')->on('subscription_plans');
$table->bigInteger('transaction_id')->unsigned();
$table->foreign('transaction_id')->references('id')->on('transactions');
$table->timestamp('subscribed_at');
$table->timestamp('expires_at')->nullable();
$table->boolean('is_active')->default(true);
$table->json('benefits');
$table->timestamps();
});
}
用户关系如下
// subscription
public function subscriptions(): HasMany
{
return $this->hasMany(Subscription::class, 'user_id')->orderBy('subscribed_at', 'asc');
}
在特定情况下,我更新了 is_active
标志。
// in user model : User.php
public function createSubscription(Plan $plan): Subscription
{
$transaction = $this->createTransaction($plan);
$data = [
'plan_id' => $plan->id,
'transaction_id' => $transaction->id,
'is_active' => true,
'subscribed_at' => now(),
'expires_at' => now()->addDays($plan->validity),
'is_active' => true,
'benefits' => $plan->benefits
];
$this->subscriptions()->update(['is_active' => false]);
return $this->subscriptions()->create($data);
}
但它正在更新所有 subscribed_at
时间戳。
请帮我解决这个问题。
将 subscribed_at 列类型从 timestamp 更改为 dateTime(或者让它为 nullable())
喜欢:
Schema::create('subscriptions', function (Blueprint $table) {
// ...
$table->dateTime('subscribed_at');
// ...
});