如何在一个月到期后将列值设置为零

时间:2017-05-31 15:04:40

标签: php mysql laravel

我有一个用户可以下载某些内容的页面。有两种类型的用户 - Basic和Premium。 基本用户每月只能下载100个内容。在DB中,我有一个包含downloadCounter列的用户表,每次用户下载列值增加1的内容时。 但是如何在每个新月将该值重置为0?可以直接在MySQL中完成,或者Laravel和Carbon有办法吗?

1 个答案:

答案 0 :(得分:1)

首先创建一个像这样的命令

php artisan make:command testcommand

现在,在app \ Console \ Commands目录中创建了testcommand.php。

打开该文件,然后像这样添加$ signature:

protected $signature = 'test_command';

您可以在文件中添加以下命令的解释。 然后你可以在文件的底部找到一个名为handle()的函数。您应该编写所有代码以重置您的计数。

public function handle()
    {
     // write code here to reset downloadCounter
    }

现在打开app \ Console \目录中的Kernal.php文件,然后添加以下内容。

protected $commands = [
    //
    Commands\testcommand::class,
];

在此之后,将以下内容添加到Schedule功能中:

protected function schedule(Schedule $schedule)
    {
         $schedule->command('test_command')->monthlyOn(1, '00:00')->timezone('Asia/Kathmandu');
    }

只需在本地环境中输入命令php artisan test_command,即可检查作业是否有效。对于实时案例,您可以调度everyMinute()来检查它的功能。

希望你明白。