如何每月根据 laravel 中的上一行自动向数据库添加新行?

How to auto add new row to database according to the previous row in laravel every month?

    ID |  User  |    Date   
------------------------------   
 1     |   Ram  |    2019-1-1  
 2     |   Ram  |    2019-2-1  
 3     |   Ram  |    2019-3-1
 4     |  Shyam |    2019-4-1
 5     |  Shyam |    2019-5-1

开头输入的是ID 1中的Ram。之后,在接下来的每个月的第一天,第 1 行需要自动添加到新行(此处为 ID 2),同时更新日期并将用户复制到下个月。如果 ram 被编辑为 shyam 的第 4 行,那么从下个月开始,编辑后的值即 shyam 应该开始自动添加到新行,并且日期也与旧日期相差 1 个月。我怎样才能完成这个任务?

您应该 create an Artisan command 获取上一行并基于该行创建下一条记录。

php artisan make:command UpdateRecord

像这样获取上一行:

$model = YourModel::orderBy('date', 'DESC')->first();
//Create new row here

然后在App\Console\Kernel中使用scheduler to run the command monthly class:

protected function schedule(Schedule $schedule) {
    $schedule->command(YourCommand::class)->monthly();
}