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?
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With