Laravel 自定义订阅更新 subscribed_at 为所有更新

发布于 2025-01-09 01:18:26 字数 1792 浏览 3 评论 0原文

我使用的是自定义订阅,这不是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫离伤花 2025-01-16 01:18:26

subscribed_at 列类型从 timestamp 更改为 dateTime (或使其可为空( ))

喜欢:

Schema::create('subscriptions', function (Blueprint $table) {

    // ...       
    
    $table->dateTime('subscribed_at');
    
    // ...

});

Change the subscribed_at column type from timestamp to dateTime (Or make it nullable())

Like:

Schema::create('subscriptions', function (Blueprint $table) {

    // ...       
    
    $table->dateTime('subscribed_at');
    
    // ...

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文