0

我正在使用 laravel 调度程序,如果满足某个条件,我想将订阅状态设置为过期,并且还需要它在后台每分钟运行一次。想出任务调度程序是最好的选择,但不知道为什么运行 php artisan schedule:work 后代码没有执行。下面是我的代码位于 App/Console/Kernel.php (调度功能)

  $schedule->call(function () {
        $checkSubStatus = UserSubscription::where('status', 'approved')->where('users_id', auth()->user()->id)->first();
        $currentTime = Carbon::now();
        if($currentTime > $checkSubStatus->expiry_date) {
            $checkSubStatus->update([
                'status' => 'expired'
            ]);
        }
    })->everyMinute();

但是当我只是删除表时有效,就像这样 DB::table('users_subscription')->delete(); 请问有什么帮助吗?

4

1 回答 1

1

您有一个由 引起的异常auth()->user()->id,因为执行闭包时没有记录任何用户。

您可以检查登录,storage/logs/laravel.log如果是这种情况,解决方案只是避免在调度程序中使用任何身份验证机制。

作为替代方案,您必须重新考虑UserSubscription应该过期的内容:

UserSubscription::where('status', 'approved')
->where('expiry_date', '<', now())
->update(['status' => 'updated');

不再有用户,您只需在日期之前的每个订阅到期。

感谢 Emre Kaya 发现错误。

于 2021-08-21T14:35:38.097 回答