【问题标题】:I want a notification that date is going to expire next week using laravel我想要使​​用 laravel 通知日期将在下周到期
【发布时间】:2021-01-19 15:36:29
【问题描述】:

我正在使用任务计划进行通知现在我正在从工作许可表中收到所有通知,但我想要一个日期将在下周到期的通知,请帮助我该怎么做?谢谢。

工作许可表

标识 |姓名 |许可代码 |日期

通知表

标识 | permit_id

app/console/commands/CreatePermitWork

class CreatePermitWork extends Command
{
  /**
   * The name and signature of the console command.
   *
   * @var string
   */
  protected $signature = 'notifications:create';

  /**
   * The console command description.
   *
   * @var string
   */
  protected $description = 'Permit work notification added !';

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

  /**
   * Execute the console command.
   *
   * @return int
   */
  public function handle()
  {
    $workpermits = WorkPermit::all();
        
    foreach ($workpermits as  $permit) {
      $ids =  array(['permit_id' => $permit->id]);
      $this->info("inserting data ".$permit->name);
      FacadesDB::table('work_permit_notifications')->insert($ids);
    }
  }
}

【问题讨论】:

    标签: php laravel


    【解决方案1】:
    $workpermits = WorkPermit::all();
    

    会给你所有的工作许可

    $workpermits = WorkPermit::where('date', '<', now()->addDays(7))->get();
    

    将为您提供日期大于小于 7 天的工作许可。

    也可以简化foreach,使用集合实例

    WorkPermit::where('date', '<', now()->addDays(7))
      ->get()
      ->each(function ($workpermit) {
            DB::table('work_permit_notifications')->insert($workPermit->id);
        });
    

    【讨论】:

      【解决方案2】:

      您必须先获取这些记录
      $havoToNotif = YourModel::where('date','&lt;',Carbon::now()-&gt;addDays(7))-&gt;get();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-14
        相关资源
        最近更新 更多