If you want to execute the migration from outside of the CLI, for example in a route, you can do so using the Artisan facade:
For the Laravel 7 and properly 6:
Use this solution:
use Illuminate\Support\Facades\Artisan;
Artisan::call('migrate');
You can pass optional parameters such as force and path as an array to the second argument in
Example:
use Illuminate\Support\Facades\Artisan;
Artisan::call('migrate', array('--path' => 'app/migrations', '--force' => true));
The --force
is necessary because Laravel will, by default, prompt you for a yes/no in production, as it’s a potentially destructive command. Without --force
, your code will just sit there spinning its wheels (Laravel’s waiting for a response from the CLI, but you’re not in the CLI).