Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 325
lewis4u Avatar asked Nov 12 '16 21:11

lewis4u


1 Answers

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!

like image 176
Risan Bagja Pradana Avatar answered Nov 15 '22 07:11

Risan Bagja Pradana