首页 > 解决方案 > Laravel 迁移到新的服务器/数据库

问题描述

我正在尝试迁移到不同主机中的新数据库。但是,当我运行时php artisan migrate,它给了我一个错误。Laravel 正在我的应用程序中寻找一个特定的表(在这种情况下是权限表,我设置了雄辩的关系)。显然,一个新的数据库是完全空的。如何在新主机中运行迁移到新数据库?我在这里想念什么?

In Connection.php line 664:

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'cf.permissions' doesn't exist (SQL: select * from `permissions`)


In PDOConnection.php line 63:

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'cf.permissions' doesn't exist


In PDOConnection.php line 61:

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'cf.permissions' doesn't exist

权限迁移文件;

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePermissionRoles extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('permissions', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('label');
            $table->timestamps();
        });

        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('label');
            $table->timestamps();
        });

        Schema::create('permission_role', function (Blueprint $table) {
            $table->integer('permission_id')->unsigned();
            $table->integer('role_id')->unsigned();

            $table->foreign('permission_id')
                    ->references('id')
                    ->on('permissions')
                   /* ->onDelete('cascade') */
;

            $table->foreign('role_id')
                    ->references('id')
                    ->on('roles')
                    /* ->onDelete('cascade') */
;

            $table->primary(['permission_id', 'role_id']);
        });

        Schema::create('role_user', function (Blueprint $table) {
            $table->integer('role_id')->unsigned();
            $table->integer('user_id')->unsigned();

            $table->foreign('role_id')
                    ->references('id')
                    ->on('roles')
                    /* ->onDelete('cascade') */
;

            $table->foreign('user_id')
                    ->references('id')
                    ->on('users')
                    /* ->onDelete('cascade') */
;

            $table->primary(['role_id', 'user_id']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('permissions');
        Schema::dropIfExists('role_user');
        Schema::dropIfExists('permission_role');
        Schema::dropIfExists('roles');
    }
}

应用服务提供者文件;

public function boot()
{
    $this->registerPolicies();

    foreach ($this->getPermissions() as $permission) {
        Gate::define($permission->name, function ($user) use ($permission) {
            return $user->hasRole($permission->roles);
        });
    }
}

protected function getPermissions()
{
    return Permission::with('roles')->get();
}

标签: laravelmigration

解决方案


在您的getPermissions()方法中,检查您的架构是否存在,permissions并使用架构的 hasTable()方法roles相应地返回值。这种方式迁移命令不会妨碍服务提供商的方法。

请注意,一旦设置了 2 个表(成功迁移后),您的服务提供商的此操作将始终执行。

<?php


protected function getPermissions(){
    if(Schema::hasTable('permissions') && Schema::hasTable('roles')){
        return Permission::with('roles')->get();
    }

    return collect([]);
}

推荐阅读