...

Migrations in Laravel (Database management)

Databases are a fundamental part of any system because they allow us to store data permanently and query it easily. Let’s learn more about Migrations in Laravel

What is a migration?

A migration is a change in the structure of the database, for example:

  • Create/Drop a table.
  • Add relations and constraints.
  • Modify data types for attributes (columns).
  • Add or remove columns in tables
  • Rename tables.

In migrations, we can also define the base data for our database, like inserting some rows in the table, but this is not recommended. We should use seeders to fill our DB.

Creating migrations in Laravel

First, we will set our database info in Laravel to start creating our migrations.

  1. Open your .env file, which is located at the root of the project, and fill in the next info:
    • DB_CONNECTION=mysql #(mysql, sqlite,…)
    • DB_HOST=127.0.0.1 # (localhost for 127.0.0.1)
    • DB_PORT=3306 #Port of the DB connection
    • DB_USERNAME=root
    • DB_PASSWORD=

Create a table migration

The new file will be at project\database\migrations\date_create_products_table

php artisan make:migration create_products_table

We will focus on the up method, we will create our table columns, see the data types here:

SQL data typeMigration typeValue Example
bigint(20) AUTO_INCREMENT->id(‘products_id’)
->id()
1,2,3,4,5….,20digit
varchar(200)->string(‘name’, length: 100);“paragraph no longer than 200 chars”
decimal->decimal(‘amount’, total: 8, places: 2);12345678.90
timestamp->timestamps();1738608430, two columns with values created_at and updated_at
integer->integer(‘votes’);2323,2,567,8755
boolean->boolean(‘confirmed’);0,1, True, False

There are more modifiers and types that we can use, you can see them here:

  • ->default(VALUE)
  • ->nullable()

Example:

public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name',100);
            $table->decimal('price', total: 8, places: 4);
            $table->integer('quantity')->default(0);
            $table->boolean('delivery')->nullable();
            $table->timestamps();
        });
    }

To run the new migrations:

php artisan migrate

If you want to drop all and reconstruct again:

php artisan migrate:fresh

Now you have your table created:

Products table in MySql

Add foreign key constraint

Let’s create another table store to set a foreign key in our products:

php artisan make:migration create_store_table
 Schema::create('store', function (Blueprint $table) {
            $table->id();
            $table->string('name',100);
            $table->string('description',100);
            $table->string('address',100);
            $table->timestamps();
        });

Now let’s create a migration to add the foreign key link from products to store:

php artisan make:migration add_migration_product_store
public function up(): void
    {
        Schema::table('products', function (Blueprint $table) {
            $table->foreignId('store_id')->constrained('store')->onUpdate('cascade')->onDelete('cascade');
        });
    }

Now our database looks the next way:

Relations established between products-store with migrations

Thank you, feel free to comment 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.