我正在尝试迁移到其他主机中的新数据库。但是,当我运行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');
}
}
AppServiceProvider文件;
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();
}
答案 0 :(得分:0)
在您的getPermissions()
方法中,检查模式permissions
和roles
是否存在,并使用Schema's hasTable()方法相应地返回值。这种迁移命令不会妨碍服务提供商的方法。
请注意,一旦设置了2个表(成功迁移之后),将始终执行服务提供商的此操作。
<?php
protected function getPermissions(){
if(Schema::hasTable('permissions') && Schema::hasTable('roles')){
return Permission::with('roles')->get();
}
return collect([]);
}