Laravel Migrations Data Types and Constraints

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. 

Author
Full Stack Developer

Deepak Talwar

Technical Architect & Full Stack Developer with 18+ years of Professional Experience in Microsoft Technologies & PHP Platform. Hands on experience with C#, VB, ASP.NET, ASP.NET MVC, ASP.NET Core, ASP.NET Web API, Linq, ADO.NET, Entity Framework, Entity Framework Core, Sql Server, MYSQL, NoSql, Javascript, Angular, jQuery, AWS, Azure, React, Angular, Laravel, Codeingiter, Serenity, VBA, Cloud Computing, Microservices, Design Patterns, Software Architecture, Web Design and Development.

Related Post