Check if input is from console

cre8 picture cre8 · May 22, 2017 · Viewed 7k times · Source

I want to share a variable of my views with:

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        \Schema::defaultStringLength(191);
        $customers = Customer::get();
        \View::share('customers', $customers);
    }
}

it works as expected, but when I want to migrate my tables via artisan it throws an error, that the table for customers was not found because it is checked BEFORE the migration starts. So I need something like

if(!artisan_request) {
    //request to laravel is via web and not artisan
} 

But I haven't found anything in the documentation.

Answer

Ian picture Ian · May 22, 2017

You can check if you are running in the console by using

app()->runningInConsole()

Underneath that, all it does is check the interface type

return php_sapi_name() == 'cli' || php_sapi_name() == 'phpdbg'

You can find more on the PHP Docs site