Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5动态运行迁移_Php_Laravel_Laravel 5 - Fatal编程技术网

Php Laravel 5动态运行迁移

Php Laravel 5动态运行迁移,php,laravel,laravel-5,Php,Laravel,Laravel 5,因此,我创建了自己的博客包,其结构为Packages/Sitemanager/blog,我有一个如下所示的服务提供商: namespace Sitemanager\Blog; use Illuminate\Support\ServiceProvider as LaravelServiceProvider; class BlogServiceProvider extends LaravelServiceProvider { /** * Indicates if loadin

因此,我创建了自己的博客包,其结构为
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';
    }
}
现在,我想做的是动态运行迁移,如果在安装过程之前或安装过程中从未运行过迁移。我在以前的文档中看到过这样的情况:

Artisan::call('migrate', array('--path' => 'app/migrations'));
然而,这在laravel 5中是无效的,我如何才能做到这一点

Artisan::call('migrate', array('--path' => 'app/migrations'));
将在Laravel5中工作,但您可能需要进行一些调整

首先,您需要一个
使用Artisan行位于文件顶部(其中
使用illumb\Support\ServiceProvider…
是)。(您也可以执行
\Artisan::call
\
非常重要)

您可能还需要执行以下操作:

Artisan::call('migrate', array('--path' => 'app/migrations', '--force' => true));
--force
是必需的,因为默认情况下,Laravel会在生产中提示您是/否,因为这是一个潜在的破坏性命令。如果没有
--force
,您的代码将只是坐在那里旋转轮子(Laravel正在等待CLI的响应,但您不在CLI中)


我鼓励您在服务提供商的
boot
方法之外的其他地方做这些事情。这些调用可能很重(依赖于文件系统和数据库调用,而您不希望在每个页面视图上都进行这些调用)。考虑到显式安装控制台命令或路由,在发布包后

php artisan供应商:发布--provider=“Packages\Namespace\ServiceProvider”

您可以使用以下方法执行迁移:

php artisan迁移

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

如果要从CLI外部(例如在路由中)执行迁移,可以使用Artisan facade:

Artisan::调用('migrate')

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

进一步阅读:

对于Laravel 7(可能还有6):


将非常有效。

现在使用我的boot命令,我可能希望在安装后运行某种安装命令(目前还不确定最好的方法)。正在执行\Artisan::调用('migrate',array('--path'=>'packages/sitemanager/blog/migrations','--force'=>true));为我工作,我可以看到对数据库所做的更改。您将如何进行安装过程?我怎样才能从这个位置恢复迁移?我可以直接调用migrate:rollback to the path吗?@mdixon18回滚可能非常糟糕-我不会自动执行。没有Artisan命令可以回滚特定的迁移,因此您可能会回滚错误的迁移并清除数据。我会通过你的应用程序提供的Artisan控制台命令进行安装。我明白了,我该如何从软件包外部的控制器加载软件包?我该如何在provider类中命名空间并调用安装方法?--它对配置和视图等的作用是否足以让包发布?此过程是否用于在不丢失数据的情况下更新(升级)已发布的项目?@ufuk迁移默认不会损害您的数据库或数据。php artisan migrate是一个很好的命令,用于更新已发布项目的数据库。请不要忘记在运行迁移之前获取备份。因此,通常情况下,在yes terminal的帮助下,我们可以轻松地进行备份,而不会丢失数据。但我如何将此应用于我的客户?你知道如何将它应用到Cpanel中的数据库吗?@ufuk在Cpanel中运行迁移不是一个好方法。但是您可以编写端点并在代码中调用
migrate
命令。在Cpanel中,人们通常手动修改数据库。我理解,但我会将应用程序提供给我的客户。我不知道他的个人信息。我应该以什么方式尝试跟踪它,以便它可以升级更新。
use Illuminate\Support\Facades\Artisan;


Artisan::call('migrate');