Showing posts with label migration. Show all posts
Showing posts with label migration. Show all posts

Tuesday, 18 March 2025

Laravel migrations, down, rollback and batch numbers

 When you create a migration using a command such as:

php artisan make:migration category

A migration file is created. As yet, the migration hasn't been made i.e. the resulting table hasn't been created. This gives you the chance to develop the migration file.

2 functions exist by default in the migration file; up() and down().

up() is commonly used to create a table.

down() is commonly used to delete the table.

When a migration has been made, it is added to the migrations table. As well as the migration name is a batch number. The batch number works in co-ordination with the down() function and the rollback command.

If you would like to go back a step, perhaps you have made a mistake, you can use the command:

php artisan migrate:rollback

This command, looks at the migrations table. It looks at the latest batch numbers and reverts the migrations with the latest batch number. It reverts those migrations by performing whatever is contained in the down() functions of those migration files.

You can see how it would be possible to selectively reverse migrations. All you have to do, is apply the latest batch number to the ones you want to reverse. 

Wednesday, 5 March 2025

Delete, add and edit columns using Laravel 12 migrations

 The following code example show how to delete, add and edit columns using Laravel 12 migrations. In all cases, when the migration has been completed, you run the command:
php artisan migrate

Delete example

php artisan make:migration delete_column_name_to_table_name --table=table_name

public function up(): void

    {

        Schema::table('table_name', function (Blueprint $table) {

            $table->dropColumn('column_name');

        });

    }


    public function down(): void

    {

        Schema::table('table_name', function (Blueprint $table) {

            $table->string('column_name'); // Define the column type to restore it

        });

    }

Add example

php artisan make:migration add_column_name_to_table_name --table=table_name

public function up(): void

    {

        Schema::table('table_name', function (Blueprint $table) {

            $table->string('column_name')->nullable();

        });

    }


    public function down(): void

    {

        Schema::table('table_name', function (Blueprint $table) {

            $table->dropColumn('column_name');

        });

    }

Edit example

php artisan make:migration edit_column_name_to_table_name --table=table_name

public function up(): void

    {

        Schema::table('table_name', function (Blueprint $table) {

            $table->string('column_name', 100)->nullable()->change();

        });

    }


    public function down(): void

    {

        Schema::table('table_name', function (Blueprint $table) {

            $table->string('column_name', 255)->notNullable()->change();

        });

    }