DEV Community

Cover image for Be careful with big increments with Laravel 6 and 7.
Ariel Mejia
Ariel Mejia

Posted on

Be careful with big increments with Laravel 6 and 7.

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.

Top comments (4)

Collapse
 
kabircse profile image
Kabir Hossain

Can I use laravel 7 for production?
I have a project implemented on laravel 5.8 then migrated to 6.2, Is it good or bad to migrate the project to laravel 7?

Collapse
 
arielmejiadev profile image
Ariel Mejia

Hi, thanks for reading the post, first Laravel has the same releases every 6 months, since laravel 5 the framework has a lot of stability, so there is a pretty good chanse that your 95% of your code works on any laravel version, some changes to review:

  • auth command is not available by default anymore.
  • all the auth classes and feature are present by default
  • to implement the same auth scaffold just add the "laravel/iu" package its an official package it adds scaffolds for vue and react
  • the "Str" class is available but str() helper maybe not

Now its important to take the time to look the new features why you could want the 7 version?

  • Implicit route model binding, powerful feature to use route model binding with different columns by adding "{user:slug}" for example.

  • blade components this feature is really great to handle views like vuejs components, with some advantage, because it compiles from php server you can event create a custom html email component and it will send as one html there is no dom js magic in this case a very powerful feature because you choose when use js components or when use blade components, additionally it has a command to create a class like "view-presenter" to handle data for this particular component, so you dont have to add variables to pass every time you want to return a view with this component.

  • Maybe the other great feature that is really helpful is the new Http class, its a guzzle wrapper but it has a lot of methods that makes easy to make a request and handle responses, status codes, json formats etc.

maybe since Laravel 5.5 this version 7 are the most cool versions, remember the only thing that change is the semantic version, but the changes are relatively minimum, if you has a large app that you need to update you has a guide in laravel docs before do the composer update command, to update the whole project, if you want a more automated tool in the market exists "Laravel shift"a company that use an API from your server provider or github to update automatically your project, this is helpful in scenarios that you have a very old version like Laravel 5.0 and you want to update it to 7, its a paid tool but it saves you weeks in just a few minutes

Collapse
 
abelmiraval profile image
abelmiraval • Edited

Hello, excellent blog. How would you migrate from increments to bigIncrements on a production system ?

Collapse
 
arielmejiadev profile image
Ariel Mejia

Thanks in production projects I just stay with the old way or in new projects I work with "BigIncrements" but you can add a new migration to add this type, but your Laravel version must be 6 at least.

If you are working on production project do this in development first and with a database engine as production before do it on production, other tip, before any db change do a db copy to restore if needed.

Thanks for reading.