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. 

Monday, 10 March 2025

Some notes on the Laravel 12 Livewire starter kit

 You can install Laraval with Livewore as a starter kit without Volt. It comes up as an option when you run:

laraval new

Having installed Livewire, you can completely ignore it and continue to develop your application with a Laravel 11 approach. 

Installing the Livewire starter kit without Volt, provides you with directories such as

app/Livewire/Auth

app/Livewire/Settings

The contents of these are Livewire components, which essentially work like controllers.

These then, have corresponding Blade files as views. The views also contain Livewire "wire" directives within for example form tags, used to make calls back to the models.

Volt enables developers not to need separate Livewire components. All the logic is contained in the Blade files.

Laravel 12 contains only the default Flux starter kit. Just because Laravel 12 uses Flux, it doesn't mean you have to only use Flux components e.g. You can simply use a HTML table with Tailwind instead of a flux:table.

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();

        });

    }

Laravel 12 Scheduling Hello world!

Many tasks require to be run every hour or day etc.  On the server this is done using a cron.

Laravel provides an easy way to create scheduled events and (importantly) how to test them before they reach the cron stage on the server.

Below is a "Hello world" example, which should offer some confidence and inspiration.

At the end of routes/console.php add the following lines:

Schedule::call(function () {

    echo "Hello World";

})->everyTwoSeconds();

Now run the command:

php artisan schedule:list

The command helpfully tells where the scheduling is going to take place.

Now let's run the task using the command:

php artisan schedule:work

After a short delay, you will see "Hello world" being outputted every 2 seconds as we stipulated.

Now you can see all sorts of options.