Laravel: Migrations (2)

Tram Ho

3. Running Migrations

To run all migrations at once, execute the following command:

Some migration activities may cause you to lose data. To protect the database when running migrations, you will be prompted to confirm before the commands are executed. To force commands to run without prompt, use the –force option:

php artisan migrate --force

a. Rolling Back Migrations To restore the latest migrate operation, you can use the rollback command. This command goes back to the last migrate, which may include multiple migrate files:

You can go back to a specific migration time by adding an option to the rollback command. For example, the following command will restore the last 5 migrations

php artisan migrate:rollback --step=5

The following command will restore all migrations:

php artisan migrate:reset

Rollback & Migrate In Single Command

The migrate: refresh command will restore all migrations and then perform migrate. This command refreshes the entire database.

You can rollback and re-migrate several times by adding an option to the refresh command. For example, the following command will restore & migrate 5 times the most recent:

php artisan migrate:refresh --step=5

Drop All Tables & Migrate

The migrate: fresh command removes all tables from the database and then executes the migrate command:

b. Tables

Creating Tables

To create a new database table, use the Schema facade creation method. This method has two arguments. The first is the name of the table, while the second is Closure that receives the Blueprint object that can be used to identify the new table:

When creating the table, you can use any of the schema builder’s column methods to identify the table columns.

Checking For Table / Column Existence

You can easily check for the existence of a table or column using the hasTable and hasColumn methods:

** Database Connection & Table Options **

If you want to perform a schema operation on a database connection that is not the default connection, use the conection method:

You can use the following commands on the schema builder to determine table options:

Commanddescription
$ table-> engine = ‘InnoDB’;Specify the table storage engine (MySQL).
$ table-> charset = ‘utf8’;Specify a default character set for the table (MySQL).
$ table-> collation = ‘utf8_unicode_ci’;Specify a default collation for the table (MySQL).
$ table-> temporary ();Create a temporary table (except SQL Server).

** Renaming / Dropping Tables ** To rename an existing database table, use the rename method:

Schema::rename($from, $to);

To remove an existing table, you can use the drop or dropIfExists methods:

Renaming Tables With Foreign Keys

Before renaming the table, you should verify that all foreign key constraints on the table have a clear name in your file instead of having Laravel assign a name based on convention. Otherwise, the foreign key constraint name will refer to the old table name.

Reference link: here.

Share the news now

Source : Viblo