-
-
Save IsmailShurrab/20b5cb7f78110d334c8b558a9dd15bb8 to your computer and use it in GitHub Desktop.
Laravel Custom Class/Model Scheduling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Don't forget to change the namespace! | |
namespace App\Traits; | |
use Cron\CronExpression; | |
use Illuminate\Support\Carbon; | |
use Illuminate\Console\Scheduling\ManagesFrequencies; | |
trait Schedulable{ | |
use ManagesFrequencies; | |
protected $expression = '* * * * *'; | |
protected $timezone; | |
public function isDue(){ | |
$date = Carbon::now(); | |
if ($this->timezone) { | |
$date->setTimezone($this->timezone); | |
} | |
return CronExpression::factory($this->expression)->isDue($date->toDateTimeString()); | |
} | |
public function nextDue(){ | |
return Carbon::instance(CronExpression::factory($this->expression)->getNextRunDate()); | |
} | |
public function lastDue(){ | |
return Carbon::instance(CronExpression::factory($this->expression)->getPreviousRunDate()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment