Laravel is a popular PHP framework that provides a robust and easy-to-use database migration system. Laravel migration allows developers to define database schema in PHP code and version control it like any other code using Git or other version control systems. Laravel migration provides a set of data types that developers can use to define their database schema. In this article, we will explore the different data types that Laravel supports in migration schema definition, along with their syntax and explanation.
Laravel Migration Data Types
Big Integer
The big integer data type is used to store a 64-bit integer value. It is typically used for primary key columns or other columns that require large integer values.
Syntax:
$table->bigInteger('column_name');
Boolean
The boolean data type is used to store a true/false value. It can be used to represent binary or logical values.
Syntax:
$table->boolean('column_name');
Char
The char data type is used to store a fixed-length string. It is useful when you know the exact length of the string that will be stored in the column.
Syntax:
$table->char('column_name', 100);
Date
The date data type is used to store a date value. It supports a wide range of date formats.
Syntax:
$table->date('column_name');
Date Time
The date time data type is used to store a date and time value. It supports a wide range of date time formats.
Syntax:
$table->dateTime('column_name');
Decimal
The decimal data type is used to store a fixed-point decimal number. It is useful when you need to store precise decimal values.
Syntax:
$table->decimal('column_name', 8, 2);
The first parameter specifies the column name, the second parameter specifies the total number of digits, and the third parameter specifies the number of decimal places.
Double
The double data type is used to store a floating-point number. It is useful when you need to store large decimal values.
Syntax:
$table->double('column_name', 8, 2);
The first parameter specifies the column name, the second parameter specifies the total number of digits, and the third parameter specifies the number of decimal places.
Float
The float data type is used to store a floating-point number. It is useful when you need to store small decimal values.
Syntax:
$table->float('column_name', 8, 2);
The first parameter specifies the column name, the second parameter specifies the total number of digits, and the third parameter specifies the number of decimal places.
Integer
The integer data type is used to store a 32-bit integer value.
Syntax:
$table->integer('column_name');
JSON
The JSON data type is used to store a JSON-encoded string. It is useful when you need to store complex data structures.
Syntax:
$table->json('column_name');
String
The string data type is used to store a variable-length string. It is useful when you do not know the exact length of the string that will be stored in the column.
Syntax:
$table->string('column_name');
Text
The text data type is used to store a large amount of text. It is useful when you need to store long strings.
Syntax:
$table->text('column_name');
Time
The time data type is used to store a time value. It supports a wide range of time formats.
Syntax:
$table->time('column_name');
Timestamp
The timestamp data type is used to store a Unix timestamp value. It is commonly used to record creation and update timestamps.
Syntax:
$table->timestamp('column_name');
Unsigned Integer
The unsigned integer data type is used to store a 32-bit unsigned integer value.
Syntax:
$table->unsignedInteger('column_name');
Unsigned Big Integer
The unsigned big integer data type is used to store a 64-bit unsigned integer value.
Syntax:
$table->unsignedBigInteger('column_name');
UUID
The UUID data type is used to store a UUID value. It is useful when you need to generate unique identifiers for your data.
Syntax:
$table->uuid('column_name');
IP Address
The IP address data type is used to store an IPv4 or IPv6 address.
Syntax:
$table->ipAddress('column_name');
MAC Address
The MAC address data type is used to store a MAC address.
Syntax:
$table->macAddress('column_name');
Binary
The binary data type is used to store binary data.
Syntax:
$table->binary('column_name');
Conclusion
In this article, we have explored the various data types that Laravel supports in migration schema definition. By using these data types, developers can easily define their database schema and version control it using Laravel’s migration system. By following Laravel’s conventions and best practices for defining database schema, developers can ensure that their application’s data is consistent, maintainable, and easily deployable.
Comments
Post a Comment