$tasks */ #[Fillable([ 'server_id', 'name', 'cron_day_of_week', 'cron_month', 'cron_day_of_month', 'cron_hour', 'cron_minute', 'is_active', 'is_processing', 'only_when_online', 'last_run_at', 'next_run_at', ])] class Schedule extends Model { /** @use HasFactory */ use HasFactory; use HasHashid; /** * The resource name for this model when it is transformed into an * API representation using fractal. */ public const string RESOURCE_NAME = 'server_schedule'; /** * Always return the tasks associated with this schedule. */ protected $with = ['tasks']; protected $attributes = [ 'name' => null, 'cron_day_of_week' => '*', 'cron_month' => '*', 'cron_day_of_month' => '*', 'cron_hour' => '*', 'cron_minute' => '*', 'is_active' => true, 'is_processing' => false, 'only_when_online' => false, ]; /** * @return array */ protected function casts(): array { return [ 'id' => 'integer', 'server_id' => 'integer', 'is_active' => 'boolean', 'is_processing' => 'boolean', 'only_when_online' => 'boolean', 'last_run_at' => 'datetime', 'next_run_at' => 'datetime', ]; } public function getRouteKeyName(): string { return $this->getKeyName(); } /** * Returns the schedule's execution crontab entry as a string. * * @throws Exception */ public function getNextRunDate(): CarbonImmutable { $formatted = sprintf('%s %s %s %s %s', $this->cron_minute, $this->cron_hour, $this->cron_day_of_month, $this->cron_month, $this->cron_day_of_week); return CarbonImmutable::instance((new CronExpression($formatted))->getNextRunDate()); } /** * Return tasks belonging to a schedule. * * @return HasMany */ public function tasks(): HasMany { return $this->hasMany(Task::class)->chaperone('schedule'); } /** * Return the server model that a schedule belongs to. * * @return BelongsTo */ public function server(): BelongsTo { return $this->belongsTo(Server::class); } }