如何更新具有条件的 table 的所有时间戳?

How to update all timestamps of a table that have a condition?

我想获取一个 table 的所有时间戳,比较所有与当前日期和所有小于当前日期的所有时间,在它们各自的时间戳上加一天,这样总是有日期更新为当天,如果最终日期已过,则明天确定。

命令:

<?php

namespace App\Console\Commands;

use App\pamatrixinf;
use App\Periodicity;
use App\Mail\SendMailable;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use Carbon\Carbon;
class Cleanminute extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'E:LD';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Every minute';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $datedi = Periodicity::select('pamatrixinf.dateen')
                ->where('pamatrixinf.cod_paperiodicity', '=', 1)
                ->get();
        $now = Carbon::now();
        if ($datedi<$now) {
           pamatrixinf::where('read_at', 1)
          ->where('cod_paperiodicity', 1)
          ->update(['read_at' => 0]);
          pamatrixinf::where('dateen','<', $now)
          ->update(['dateen' => 'dateen'->addDay()]);
        }


    }
}

正如您在我的 table 中看到的那样,我有 read_at 字段来了解它是否被看到(用于另一个功能)和 dateen 用于告诉我每项工作的截止日期我想用它来比较我的 $ now 变量,我希望你能帮助我。 由于我 运行 命令没有任何错误,但没有任何反应。为了让他们或多或少知道我想要发生什么,我举个例子。

我还是不执行命令:

id dateen  

1 2020-06-03 14:00:00.000000
2 2020-07-05 14:00:00.000000
3 2020-06-07 14:00:00.000000

我执行命令:

id dateen  

    1 2020-06-04 14:00:00.000000  
    2 2020-07-05 14:00:00.000000  
    3 2020-06-08 14:00:00.000000

感谢帮助

由于您使用的是 get(),因此 return 值是一个集合,因此您需要使用 foreach 循环遍历该集合,如果您在 table 中只有一条记录可以使用 first() 并且您不再需要使用 foreach 循环:

示例:

  public function handle()
    {
        $dates = Periodicity::select('pamatrixinf.dateen')
                            ->where('pamatrixinf.cod_paperiodicity', '=', 1)
                            ->get();
        $now = Carbon::now();
        foreach($dateas as $datedi){
         if ($datedi->dateen < $now) {
           
              pamatrixinf::where('read_at', 1)
              ->where('cod_paperiodicity', 1)
              ->update(['read_at' => 0]);
           
              pamatrixinf::where('dateen','<', $now)
              ->update(['dateen' => 'dateen'->addDay()]);
          }
        }
    }

首先

   public function handle()
    {
        $datedi = Periodicity::select('pamatrixinf.dateen')
                ->where('pamatrixinf.cod_paperiodicity', '=', 1)
                ->first();
                
        $now = Carbon::now();
        if ($datedi->dateen < $now) {
           pamatrixinf::where('read_at', 1)
          ->where('cod_paperiodicity', 1)
          ->update(['read_at' => 0]);
          pamatrixinf::where('dateen','<', $now)
          ->update(['dateen' => 'dateen'->addDay()]);
        }


    }