run artisan command from controller

Code Example - run artisan command from controller

                
                        If you have simple job to do you can do it from route file. 
For example you want to clear cache. In terminal it would be php artisan 
cache:clear In route file that would be:

\Artisan::call('cache:clear');
                    
                
 

execute artisan command from route

                        
                                Route::get('clear_cache', function () {

    \Artisan::call('cache:clear');

    dd("Cache is cleared");

});
                            
                        
 

artisan command in route

                        
                                use Illuminate\Support\Facades\Artisan;

Route::post('/user/{user}/mail', function ($user) {
    $exitCode = Artisan::call('mail:send', [
        'user' => $user, '--queue' => 'default'
    ]);

    //
});
                            
                        
 

laravel create command tutorial

                        
                                Artisan::command('build {project}', function ($project) {
    $this->info("Building {$project}!");
})->describe('Build the project');