Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Laravel Custom Class/Model Scheduling
//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) {
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());
Copy link

simonhamp commented Feb 22, 2018

Thanks for sharing!

Copy link

karlmonson commented Feb 27, 2018

Thank you for this trait!

Copy link

adamlevenson commented Aug 6, 2018

Thanks for this! Exactly what I needed, gave me the right path forward.

Copy link

digitiqman commented Jun 13, 2020

Thank you David, you are the best!

Copy link

jakubgrzaslewicz commented Mar 7, 2021

Thank you. Excellent solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment