laravel - 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();
}
解决方案
在您的getPermissions()
方法中,检查您的架构是否存在,permissions
并使用架构的 hasTable()方法roles
相应地返回值。这种方式迁移命令不会妨碍服务提供商的方法。
请注意,一旦设置了 2 个表(成功迁移后),您的服务提供商的此操作将始终执行。
<?php
protected function getPermissions(){
if(Schema::hasTable('permissions') && Schema::hasTable('roles')){
return Permission::with('roles')->get();
}
return collect([]);
}
推荐阅读
- excel - 在 Range().Select 中引用单元格作为变量
- ibm-midrange - 在 AS400 中显示新窗口时如何删除上一个窗口
- java - Spark 作业 JAVA RDD 性能所有数据都到一个执行器节点
- angular - PrimeNg:手风琴标签不会自动关闭
- javascript - 如何在数据库中检索和插入 MySQL 数据使用 PHP 和 JQuery AJAX 标记?
- node.js - 从两个单独的模型中获取数据并将它们显示在我的页面上
- c# - 在 c#.net 中添加服务引用时出错
- node.js - 在 Node 中存储敏感凭证的最佳方式是什么?
- c# - Unity:Shooter 2D-Character 动画只运行一次
- sql - SQL 在列之间交换值