Use environments() on scheduled Artisan commands to restrict task execution strictly to specific environments (like production or staging).
Certain scheduled tasks (such as customer billing charges, production backup uploads, or external status pings) should only ever run in production, while sandbox synchronization jobs may only belong on staging or local environments.
Laravel's task scheduler provides the environments() constraint.
Restricting Tasks to Specific Environments
In routes/console.php or app/Console/Kernel.php:
use Illuminate\Support\Facades\Schedule;
// Runs only on production
Schedule::command('billing:charge-monthly-invoices')
->monthlyOn(1, '00:00')
->environments(['production']);
// Runs on staging and local only
Schedule::command('sandbox:refresh-test-data')
->daily()
->environments(['staging', 'local']);
Dynamic Conditional Scheduling with when()
For custom runtime checks beyond environment names:
Schedule::command('reports:generate')
->daily()
->when(function () {
// Runs only if reporting feature flag is enabled
return config('features.reporting_enabled') === true;
});
Summary
- Prevents accidental execution of production jobs on local or staging servers.
- Accepts an array of environment strings matching
APP_ENV. - Cleanly replaces manual
if (app()->isProduction())checks inside scheduled closures.
Related Tips
View all tips →Monitor Aggregate Queue Metrics with Total Size Helpers in Laravel 13.27
Laravel 13.27 adds totalPendingSize(), totalDelayedSize(), and totalReservedSize() to the Queue facade, making it easy to aggregate queue metrics for dashboards and health checks.
Centralized Queue Routing with Queue::forward() in Laravel 13.26
Laravel 13.26 introduces Queue::forward() to reroute queued jobs to new queue names, different connections, or both without modifying job classes or dispatch sites.