Laravel 5 动态运行迁移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37953783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:02:50  来源:igfitidea点击:

Laravel 5 dynamically run migrations

phplaravellaravel-5

提问by mdixon18

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

所以我创建了自己的博客包,其结构为Packages/Sitemanager/Blog我有一个服务提供者,如下所示:

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'));

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

但是,这在 laravel 5 中是无效的,我该如何处理?

回答by ceejayoz

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

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

将在 Laravel 5 中工作,但您可能需要进行一些调整。

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

首先,由于 Laravel 5 的命名空间,您需要use Artisan;在文件顶部(在哪里use Illuminate\Support\ServiceProvider...)添加一行。(你也可以这样做\Artisan::call- 这\很重要)。

You likely also need to do this:

您可能还需要这样做:

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

The --forceis 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 inthe CLI).

--force是必要的,因为 Laravel 默认会在生产环境中提示您输入是/否,因为这是一个潜在的破坏性命令。如果没有--force,你的代码将只是坐在那里旋转的轮子(Laravel的,等待来自CLI的响应,但你不是命令行)。

enter image description here

在此处输入图片说明

I'd encourage you to do this stuff somewhere otherthan the bootmethod 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.

我鼓励您在服务提供商的方法之外的其他地方做这些事情boot。这些可能是繁重的调用(依赖于您不想在每次网页浏览时进行的文件系统和数据库调用)。考虑使用显式安装控制台命令或路由。

回答by SArnab

After publishing the package:

发布包后:

php artisan vendor:publish --provider="Packages\Namespace\ServiceProvider"

php artisan vendor:publish --provider="Packages\Namespace\ServiceProvider"

You can execute the migration using:

您可以使用以下命令执行迁移:

php artisan migrate

php artisan migrate

Laravel automatically keeps track of which migrations have been executed and runs new ones accordingly.

Laravel 会自动跟踪已执行的迁移并相应地运行新的迁移。

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:

如果您想从 CLI 外部执行迁移,例如在路由中,您可以使用 Artisan 门面执行此操作:

Artisan::call('migrate')

Artisan::call('migrate')

You can pass optional parameters such as force and path as an array to the second argument in Artisan::call.

您可以将可选参数(例如 force 和 path)作为数组传递给 Artisan::call 中的第二个参数。

Further reading:

进一步阅读:

回答by Amin Shojaei

For the Laravel 7:

对于 Laravel 7:

use Illuminate\Support\Facades\Artisan;


Artisan::call('migrate');

will greatly work.

会很有用。