Ryan Chandler

Uncategorized

How to Schedule Artisan Commands from Your Laravel Package

1 min read

This post was published 3 years ago. Some of the information might be outdated!

Before you can run your command on the scheduler, you need to make sure you've actually registered the command with artisan.

To do this, use the $this->commands method in your ServiceProvider::boot method.

class MyPackageServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->commands([
            Commands\MyAwesomeCommand::class,
        ])
    }
}

By doing this, you can now run your command using php artisan my-awesome-command.

Scheduling the command

Now that artisan is aware of your command, you can hook it up to Laravel's Schedule and run it as often as you need.

Begin by calling $this->app->afterResolving in your ServiceProvider::boot method, passing through two arguments.

The first argument should be the abstract that is being resolved. In this case, that will be Illuminate\Console\Scheduling\Schedule. The second argument should be a Closure that accepts the object in its parameter list.

use Illuminate\Console\Scheduling\Schedule;

class MyPackageServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->commands([
            Commands\MyAwesomeCommand::class,
        ])
		  
        $this->app->afterResolving(Schedule::class, function (Schedule $schedule) {
            $schedule->command(Commands\MyAwesomeCommand::class)->everyMinute();
        });
    }
}

Now when you run php artisan schedule:run, the Closure will be executed and MyAwesomeCommand will be added to the scheduler, running once every minute.