Heim  >  Fragen und Antworten  >  Hauptteil

Laravel ändert automatisch den Status in der Datenbank, nachdem das Datum abgelaufen ist

Ich habe tasks 表,该表有 statusdeadline Spalten. Wie ändere ich den Status automatisch auf „Abgelaufen“, wenn das aktuelle Datum größer ist als das Fälligkeitsdatum der Aufgabe? Gibt es Echtzeit-Ereignis-Listener in Laravel?

Ich denke, so sollte die Event-Listener-Klasse aussehen, bin mir aber nicht sicher, was ich als Nächstes tun soll.

<?php
 
namespace AppEvents;
 
use AppModelsTask;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateFoundationEventsDispatchable;
use IlluminateQueueSerializesModels;
 
class DeadlineExpired
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
 
    /**
     * The task instance.
     *
     * @var AppModelsTask
     */

    public $task;
 
    /**
     * Create a new event instance.
     *
     * @param  AppModelsTask $task
     * @return void
     */
    public function __construct(Task $task)
    {
        $this->task = $task;
    }
}


P粉447002127P粉447002127283 Tage vor477

Antworte allen(3)Ich werde antworten

  • P粉683665106

    P粉6836651062023-12-16 11:39:22

    有实时事件侦听器,但需要执行操作才能触发。例如,当创建、更新或删除模型时,就会触发这些事件。

    没有内置的“侦听器”来 ping 每个等待您定义的字段更改的模型。

    如果您想在任务过期时触发进一步的逻辑(例如发送电子邮件),那么您最好使用调度程序检查是否有任何新的过期任务。 调度程序每分钟运行一次 - 由 cron 设置。

    Antwort
    0
  • P粉092778585

    P粉0927785852023-12-16 10:27:05

    因为您只检查日期。你的 cron 只需要在午夜运行一次。使用 Laravel Scheduler 来完成您的工作。 首先创建一个类

    class UpdateTasks
    {
        public function __invoke()
        {
            // do your task here...e.g.,
            Tasks::whereDate('deadline','<',today())->update(['status'=>'expired']);
        }
    }

    然后在你的app\Console\Kernel.php中,schedule方法-

    $schedule->call(new UpdateTasks())->daily();

    最后在您的服务器上配置一个 cron 作业以每天午夜运行计划命令。

    php artisan schedule:run

    Antwort
    0
  • 似水

    似水2023-12-16 15:18:34

    你的问题我用GPT帮你解答  希望有用 

    606915a3b01db9aab05d290248494e3.jpg

    Antwort
    0
  • StornierenAntwort