Skip to content
Advertisement

Laravel 5 dynamically run migrations

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:

JavaScript

Now, what i would like to do is run the migrations dynamically if they have never been run before or within an installation process i suppose. I’ve seen in older documentation you could so something like this:

JavaScript

However, this is invalid in laravel 5, how can I approach this?

Advertisement

Answer

JavaScript

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 IlluminateSupportServiceProvider... is), because of Laravel 5’s namespacing. (You can alternatively do Artisan::call – the is important).

You likely also need to do this:

JavaScript

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

enter image description here

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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement