The introduction

After table creation and migration rollback using Laravel’s database migration functionality, we move on to what happens after table structure changes are made in the design. And how to populate the database with some pseudo-data as a test.

Data constraints

In the previous chapter on database migration rollback, you can manually control which batch of migration locations to roll back to. For example, the events table we created would look like this if we added a field to the migration file:

We added a venue field in the middle of the database table. Then use the migration directive

php artisan migrate
Copy the code

You can apply these changes.

Most of the time, it’s not as if we created the table, specified the field name, and the field data type. We need null and non-null constraints, default value constraints, primary key constraints, foreign key constraints, and so on. That’s the beauty of relational databases.

For example, declare a Boolean value and declare that the default is false:

$table->boolean('confirmed')->default(false);
Copy the code

For example, set a string field to null:

$table->string('comments')->nullable();
Copy the code

For example, setting an integer to an unsigned number:

$table->tinyInteger('age')->unsigned();
Copy the code

Set the default value for the age field above:

$table->tinyInteger('age')->unsigned()->default(0);
Copy the code

Add or delete fields

Create a migration file using the migration function to add and delete fields from a database table. Create a migration file using the following scaffolding instruction:

php artisan make:migration add_enabled_to_events_table --table=events
Copy the code

In this migration file we will add a enabled Boolean field for the events table. The output of the above instructions is as follows:

Created Migration: 2020 _09_28_213116_add_enabled_to_events_table
Copy the code

Now add up & Down methods to the migrated file, specifying the actions to be performed to start the migration and to rollback. We simply add a field, or when rollback, remove a field, as shown below:

Execute the migration command on the command line after completion:

php artisan migrate
Copy the code

The following output is displayed:

Migrating: 2020 _09_28_213116_add_enabled_to_events_table
Migrated: 2020 _09_28_213116_add_enabled_to_events_table
Copy the code

We did not specify which field to add after, so laravel assembles SQL after the last field by default. SQL statement append field syntax:

ALTER TABLE contacts ADD last_name varchar(40) NOT NULL AFTER contact_id;
Copy the code

Laravel’s migration directive also allows us to specify an appended field after a column:

$table->boolean('enabled')->after('name');
Copy the code

Migration status

Viewing the migration status of the current database allows us to see which migration files have been applied and which have not been applied, which can effectively troubleshoot problems in the case of large database tables.

Laravel provides scaffolding commands:

php artisan migrate:status
Copy the code

The output looks like the following:

The magrate:reset command is used to rollback all migrations, unlike migrate:rollback. There is also migrate:refresh. If you make changes to some migrated files, this command will refresh all changes and apply them.

Use both with caution!

Write in the last

This article complements the laravel database migration functionality described in the previous chapter. Database migration is a big operation, especially for the application database that has been put into production. If it has to be updated and migrated, it is necessary to prepare for emergencies.

Happy coding 🙂

I am a @programmer assistant, an original author in the IT field who focuses on programming knowledge and circle dynamics