Laravel Queues in Action (2nd edition) is now available!

The problem behind Undefined constant 'STDIN' when running Laravel artisan command programmatically

Updated: Nov 13, 2020 — 1 min Read#quick-dip

Undefined constant 'STDIN'
/tmp/vendor/symfony/console/Helper/QuestionHelper.php:110

You will see this error if you try to run some artisan commands programmatically from within an HTTP request:

Route::get('/queue-clear', function () {
    Artisan::call('queue:clear');
});

This is because Laravel will try to prompt for confirmation if the application is running in the production environment. While doing so, the Symfony/Console component will use the built-in STDIN PHP constant to capture the input stream. That constant is only available in the CLI environment.

To avoid this error, you need to use the --force option to disable the interactivity:

Route::get('/queue-clear', function () {
    Artisan::call('queue:clear', [
       '--force' => true
    ]);
});

Hey! 👋 If you find this content useful, consider sponsoring me on GitHub.

You can also follow me on Twitter, I regularly post about all things Laravel including my latest video tutorials and blog posts.

By Mohamed Said

Hello! I'm a former Laravel core team member & VP of Engineering at Foodics. In this publication, I share everything I know about Laravel's core, packages, and tools.

You can find me on Twitter and Github.

This site was built using Wink. Follow the RSS Feed.