Laravel 5 dynamically run migrations

前端 未结 3 1272
天命终不由人
天命终不由人 2021-01-05 02:11

so I have created my own blog package in a structure of Packages/Sitemanager/Blog I have a service provider that looks like the following:

names         


        
3条回答
  •  执念已碎
    2021-01-05 02:23

    Artisan::call('migrate', array('--path' => 'app/migrations'));
    

    will work in Laravel 5, but you'll likely need to make a couple tweaks.

    First, you need a use Artisan; line at the top of your file (where use Illuminate\Support\ServiceProvider... is), because of Laravel 5's namespacing. (You can alternatively do \Artisan::call - the \ is important).

    You likely also need to do this:

    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).

    I'd encourage you to do this stuff somewhere other than the boot method of a service provider. These can be heavy calls (relying on both filesystem and database calls you don't want to make on every pageview). Consider an explicit installation console command or route instead.

提交回复
热议问题