How to run laravel migrations without artisan (usi

2019-04-14 06:11发布

问题:

I recently hosted a laravel project (for a customer) on shared hosting, after failed attempts to get access to the server via ssh I contacted the host who informed me that ssh service was not available for my customers hosting plan, that means I have no access to terminal and can't use artisan. I know how to write a php script that will create sql tables but just before that I was wondering if theres a shortcut to this with laravel since the migrations(tables) are already defined. What I want is like to create a route project.com/run_migrations to do the job! Thanks in advance

回答1:

You can easily create a small Artisan script within PHP like this:

Artisan::call('migrate');

This equals php artisan migrate. Use it anywhere you want to run your migrations.

If you are in production mode (if APP_ENV=production inside your .env file) then you would have to force the migration in case you want to allow to make changes. You can do it as follows:

Artisan::call('migrate', ["--force"=> true ]);

This equals of adding the --force flag a la php artisan migrate --force.

To answer your specific question though, create a route like this:

Route::get('/run-migrations', function () {
    return Artisan::call('migrate', ["--force"=> true ]);
});

If you are interested in creating a web installer, you might be interested in this package:

https://github.com/Froiden/laravel-installer

Check out the code to see how he handles migrations and seeds etc.