foreign key retry incorrectly formed laravel migration

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

class CreateMenusTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {        
        Schema::create('menu', function (Blueprint $table) {
            $table->increments('id');

            $table->string('opcao');
            $table->string('href');
            $table->boolean('session');
            $table->timestamps();
        });

        Schema::create('submenu', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('submenu_id')->index;
            $table->foreign('submenu_id')->references('id')->on('menu');
            $table->string('opcao');
            $table->string('href');
            $table->boolean('ligado');
            $table->timestamps();
        });

    }} 

is giving malformed foreign key constraint error. Someone can help me. This error occurs when I run the command php artisan migrate .

    
asked by anonymous 02.02.2018 / 20:13

1 answer

0

Failed to set unsigned to submenu_id so that migration is true, because, the primary key of the menu table is created as unsigned which means that the field only accepts positive values, so relationship also needs to know this and configured for it. Example:

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

class CreateMenusTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {        
        Schema::create('menu', function (Blueprint $table) {
            $table->increments('id');

            $table->string('opcao');
            $table->string('href');
            $table->boolean('session');
            $table->timestamps();
        });

        Schema::create('submenu', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('submenu_id')->unsigned();
            $table->foreign('submenu_id')->references('id')->on('menu');
            $table->string('opcao');
            $table->string('href');
            $table->boolean('ligado');
            $table->timestamps();
        });

    }
} 
    
03.02.2018 / 14:00