Laravel Migrations Data Types and Constraints
- Posted on
- Laravel
- By Deepak Talwar
Laravel Migrations Data Types and Constraints
Laravel has many data kinds and limitations. These data types and restrictions can simply define database table structure in migrations.

- $table->id(): Auto-incrementing primary key.
- $table->string(‘column_name’): A 255-character string column. For integer columns, use
- $table->integer(‘column_name’).
- $table->timestamps(): Adds created_at and updated_at columns automatically.
- $table->foreignId(‘column_name’): A foreign key column that references another table's id column.
Constraints enforce rules on table data to maintain data integrity:
- The primary key is $table->primary(‘column_name’).
- Foreign Key: $table->foreign(‘column_name’)->references(‘id’)->on(‘other_table’)
- Unique constraint: $table->unique(‘column_name’).
- The index is $table->index(‘column_name’).
Laravel Migration streamlines Laravel database management. It supports version control, database portability, collaboration, rollback, consistency, and documentation.
Add size to field
$table->string('name_logistic_co', 100)->nullable();
Add Default value to existing column
Schema::table('product', function (Blueprint $table) {
$table->Integer("level")->default(0)->change();
});
Change Column Position
public function up(): void{
Schema::table('product', function (Blueprint $table) {
$table->integer('is_publish')->after('order')->change();
});
We discussed some elements of utilizing migrations.