Be careful with big increments with Laravel 6 and 7.

Ariel Mejia - Jun 1 '20 - - Dev Community

From Laravel 5.x to Laravel 6 by the default the migrations has a little change now all id columns has a type of big increments unsigned:

Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements();
    ...
});
Enter fullscreen mode Exit fullscreen mode

Then the foreign key requires to be an "UnsignedBigInteger":

Schema::create('posts', function (Blueprint $table) {
    $table->unsignedBigInteger('user_id');
    ...
});
Enter fullscreen mode Exit fullscreen mode

A little note on Laravel 7.

It adds a helper method to be an "alias" of "UnsignedBigInteger", now in your migrations by default you will have the id column:

Schema::create('users', function (Blueprint $table) {
    $table->id();
    ...
});
Enter fullscreen mode Exit fullscreen mode

The id method in is just an alias of the same "UnsignedIncrements", so nothing has change is just a method to make more readable the migrations code.

Define a foreign keys with Laravel 7:

Schema::create('posts', function (Blueprint $table) {
    $table->foreign_id('user_id')->references('id')->on('users');
    ...
});
Enter fullscreen mode Exit fullscreen mode

But you have an even better way o add this foreign key, if you follow the convention, (id is the name of column id and use {tablename}_id as the foreign key):

Schema::create('posts', function (Blueprint $table) {
    $table->foreign_id('user_id')->constrained();
    ...
});
Enter fullscreen mode Exit fullscreen mode

That's all, thanks for reading.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .