Laravel 5.5 set size of integer fields in migration file
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Setting Field Lengths in Laravel Migration Files: A Comprehensive Guide
Body:
When using Laravel to create tables with migrations, it is essential to understand how to control the size of various field types. While Laravel does not provide direct options for setting field lengths within migrations, there are workarounds and best practices that can be followed to ensure proper length specifications.
Working with Integer Fields
In your example code snippet, you have the following migration:$table->increments('id')->length(11);
$table->integer('bank_id')->length(11);
As you can see, Laravel provides the increments method for auto-incrementing fields, and you have customized its length to 11. However, when dealing with integer fields like bank_id, your code specifies a length of 11, but Laravel generates it as 4 by default. To change this behavior, you can define the field length explicitly in your model class:
class YourModel extends Model {
/**
* Get the primary key type.
*/
public function getPrimaryKeyType()
{
return 'integer'; // Default is 'bigInteger'
}
}
Alternatively, you can choose to use a different field type that supports custom sizes. For example, you could utilize the smallInteger or tinyInteger types:
$table->smallInteger('bank_id');
$table->tinyInteger('is_black');
Each of these field types has a maximum length assigned by default. For tinyInteger, it is 1 byte, while for smallInteger, it is 2 bytes. To increase the size to match your desired custom lengths, you can define them in your model class as well:
class YourModel extends Model {
/**
* Get the primary key type.
*/
public function getPrimaryKeyType()
{
return 'tinyInteger'; // Default is 'bigInteger'
}
}
Possible Pitfalls and Alternative Solutions
Setting custom field lengths in your model classes should generally be avoided, as it may cause compatibility issues with later Laravel versions. A better approach would be to set the appropriate field types that support custom lengths during migration creation. For instance:$table->tinyInteger('is_black');
Alternatively, you could store boolean values as true/false in your database and use a separate table for categorical data, such as colors. This would allow you to set the appropriate length based on your needs without relying on predefined types that may change over time.
In conclusion, managing field lengths in migration files can be achieved by setting custom lengths within your model class or choosing appropriate field types during migration creation. By carefully considering the impact of these decisions and weighing the pros and cons, you will be better equipped to manage field sizes effectively and avoid potential compatibility issues.