lewis4u lewis4u - 22 days ago 6
PHP Question

Laravel 5.3 - Is it possible to run php artisan command by clicking on a link/button

So as the question says...is there a way to run php artisan command in background when user clicks on a link in web browser?

For example i would like to make a button in my app for migrating migration files so when this button is clicked:

<a href="/migrate" class="btn btn-primary">Migrate</a>


i would like to run

php artisan migrate


in background.

Is this somehow possible?

Answer

Sure you can! Just simply create a new route within your routes\web.php file. Then you can just simply call Artisan::call() method.

For example, when you visit make-migration route, you want to create a migration file for Invoices table. You can do this like so:

Route::get('make-migration', function () {
    Artisan::call('make:migration', [
        'name' => 'create_invoices_table',
        '--create' => 'invoices',
    ]);

    return 'Create invoices migration table.';
});

Or in your case, if you want to run the migration:

Route::get('migrate', function () {
    Artisan::call('migrate');

    return 'Database migration success.';
});

Read more about running Artisan command programmatically here.

Hope this help!

Comments