Mysql 无法使用Laravel 5.2中的外键进行迁移

Mysql 无法使用Laravel 5.2中的外键进行迁移,mysql,laravel-5,foreign-keys,database-migration,laravel-5.2,Mysql,Laravel 5,Foreign Keys,Database Migration,Laravel 5.2,我想创建两个表用户和角色。我的代码如下: 2014\u10\u12\u000000\u创建用户\u表格.php <?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @re

我想创建两个表
用户
角色
。我的代码如下:

2014\u10\u12\u000000\u创建用户\u表格.php

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->integer('role')->unsigned();
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();

            $table->foreign('role')
                ->references('id')
                ->on('roles');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}
<?php

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

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

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

您应该确保在
用户
表迁移之前,您的
角色
表迁移正在运行。默认情况下,在Laravel中,您已经创建了
用户
表迁移,因此,如果只更改其代码,然后添加
角色
迁移,它将无法工作。您应该更改
用户
角色
迁移文件名,以确保
角色
表迁移文件开头的时间戳位于
用户
表之前

例如,您可能遇到如下情况:

2014_10_12_000000_create_users_table.php
2015_10_12_123552_create_roles_table.php
2015_10_12_123652_create_users_table.php
2015_10_12_123552_create_roles_table.php
您应该重命名文件,使其如下所示:

2014_10_12_000000_create_users_table.php
2015_10_12_123552_create_roles_table.php
2015_10_12_123652_create_users_table.php
2015_10_12_123552_create_roles_table.php
当然,我假设您只在开发期间使用这些迁移,而它还没有投入生产

 public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('firstname');
            $table->string('lastname');
            $table->text('slug');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->decimal('fidbonus', 6, 2);
            $table->rememberToken();
            $table->timestamps();
        });
        Schema::create('Userinfos', function (Blueprint $table) {
           $table->increments('id');
           $table->integer('User_id')->unsigned();
           $table->foreign('User_id')->references('id')->on('Users');
           $table->string('address');
           $table->string('address2');
           $table->text('city');
           $table->string('zip');
           $table->string('country');
           $table->string('Phone');
           $table->timestamps();
       });
       Schema::create('password_resets', function (Blueprint $table) {
           $table->string('email')->index();
           $table->string('token')->index();
           $table->timestamp('created_at');
       });
    }

尝试上面的代码。它工作正常

@smartrahat没有问题。您需要记住,您只能在现有表中创建外键,因此有时您需要创建2次迁移(不带外键),并在第3次迁移中在表之间添加外键。感谢您的提示。我会记住的。我认为我们不能写超过1个'up()'方法。如果需要,在相同的“up()”方法中,您可以使用“Schema::create()”两次,一次在另一次下面。是的,我复制并粘贴了错误的内容,但仅此而已,我将对其进行编辑并从我自己的项目中添加示例,谢谢