Laravel 5.8 and bigIncrements issues

Laravel 5.8 and bigIncrements issues

Since Laravel 5.8 released there was a big change that wasn't mentioned in Laravel documents nor in Laravel official social media accounts.

Laravel 5.8 comes with bigIncrements as default.

This mostly cause problems when you try to use integer columns in another tables and it will throw error while migrating your schemas.

 

Default schema in Laravel 5.8:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
       });
}
PHP

 

As you see id column now is set to bigIncrements.

 

Lets assume that you want to make profile table and you need to assign user_id for that table, here is how you do it:

 

public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('photo');
            $table->bigInteger('user_id')->unsigned()->nullable();
            $table->timestamps();
        });
        Schema::table('profiles', function (Blueprint $table) {
              $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
        });
}
PHP

 

As you see I made my user_id column set to bigInteger type why bigInteger instead of integer? because my users table id's are bigIncrements.

To remember that easily try this:

bigIncrements = bigInteger  (big = big)

increments = integer (not incude big = not include big)
Ini

easy right?

 

Another point of why I used Schema::table instead of making my foreginer relation inline is to avoid artisan error while executing migration command (I used 2 step)

- Last updated 1 year ago

Be the first to leave a comment.

You must login to leave a comment