Laravel 自定义订阅更新 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
时间戳。 请帮我解决这个问题。
I am using a customized subscription which is not laravel's default cashier.
and the migration looks as below
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();
});
}
and the user relation as below
// subscription
public function subscriptions(): HasMany
{
return $this->hasMany(Subscription::class, 'user_id')->orderBy('subscribed_at', 'asc');
}
on a specific case, I update the is_active
flag.
// 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);
}
but it's updating all the subscribed_at
timestamps.
Please help me to solve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 subscribed_at 列类型从 timestamp 更改为 dateTime (或使其可为空( ))
喜欢:
Change the subscribed_at column type from timestamp to dateTime (Or make it nullable())
Like: