Sign Up Form

Sign Up

How do I create a new migration?

310 163 point-admin
  • 0

Migrations in Laravel provide a way to modify and manage your database schema over time. To create a new migration, follow these steps:

1. Run the Artisan Command

Use the Artisan CLI to create a migration file:

bashCopy codephp artisan make:migration create_users_table

Here, create_users_table is the name of the migration, and this command generates a migration file in the database/migrations folder.

2. Define the Schema

Inside the generated migration file, you can define the table structure using the up method. Laravel provides schema building methods:

phpCopy codepublic function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamps();
    });
}

The down method is used to reverse the migration, like dropping the table:

phpCopy codepublic function down()
{
    Schema::dropIfExists('users');
}

3. Run the Migration

Once the migration file is ready, run the migration using:

bashCopy codephp artisan migrate

This will execute the migration and modify the database accordingly.

4. Roll Back Migrations

If you need to undo the migration, you can use:

bashCopy codephp artisan migrate:rollback

This will reverse the most recent migration.

Conclusion

Creating and managing migrations in Laravel is a simple and structured way to evolve your database schema over time. With the make:migration command, you can easily define new tables, columns, and relationships, keeping your schema versioned and consistent.

1 comment

Leave a Reply

Your email address will not be published.