Laravel 5动态运行迁移 [英] Laravel 5 dynamically run migrations
问题描述
所以我以Packages/Sitemanager/Blog
的结构创建了自己的博客包,我有一个如下所示的服务提供商:
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:
namespace Sitemanager\Blog;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
class BlogServiceProvider extends LaravelServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot() {
$this->handleConfigs();
$this->handleMigrations();
$this->handleViews();
$this->handleRoutes();
}
/**
* Register the service provider.
*
* @return void
*/
public function register() {
// Bind any implementations.
$this->app->make('Sitemanager\Blog\Controllers\BlogController');
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides() {
return [];
}
private function handleConfigs() {
$configPath = __DIR__ . '/config/blog.php';
$this->publishes([$configPath => config_path('blog.php')]);
$this->mergeConfigFrom($configPath, 'blog');
}
private function handleTranslations() {
$this->loadTranslationsFrom(__DIR__.'/lang', 'blog');
}
private function handleViews() {
$this->loadViewsFrom(__DIR__.'/views', 'blog');
$this->publishes([__DIR__.'/views' => base_path('resources/views/vendor/blog')]);
}
private function handleMigrations() {
$this->publishes([__DIR__ . '/migrations' => base_path('database/migrations')]);
}
private function handleRoutes() {
include __DIR__.'/routes.php';
}
}
现在,我想做的是,如果我想在安装过程之前或之内从未运行过迁移,则可以动态地运行迁移.我在较旧的文档中已经看到,您可以这样:
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:
Artisan::call('migrate', array('--path' => 'app/migrations'));
但是,这在laravel 5中是无效的,我该如何处理呢?
However, this is invalid in laravel 5, how can I approach this?
推荐答案
Artisan::call('migrate', array('--path' => 'app/migrations'));
在Laravel 5中可以使用,但是您可能需要进行一些调整.
will work in Laravel 5, but you'll likely need to make a couple tweaks.
首先,由于Laravel 5的命名空间,您需要在文件顶部(use Illuminate\Support\ServiceProvider...
所在的位置)的顶部加上use Artisan;
行. (您也可以选择\Artisan::call
-\
很重要).
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).
您可能还需要这样做:
Artisan::call('migrate', array('--path' => 'app/migrations', '--force' => true));
--force
是必需的,因为默认情况下,Laravel会提示您在生产中选择是/否,因为这是潜在的破坏性命令.如果没有--force
,您的代码就会停滞不前(Laravel正在等待CLI的响应,但您不在CLI中 ).
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).
我建议您在服务提供者的boot
方法之外的其他地方 处执行此操作.这些可能是繁重的调用(依赖于您不想在每个综合浏览量上进行的文件系统和数据库调用).考虑使用显式的安装控制台命令或路由.
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.
这篇关于Laravel 5动态运行迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!