I have a use case where we need to modify application flow if the application is being run from the command line via Artisan (migrations, seeds, route:list).
Anyone can use laravel app() helper function to avoid any namespace related issue. So to check if script is running in cli or browser, can be using this line of code app()->runningInConsole()
Basic usage:
if ( app()->runningInConsole() ){
// it's console.
}