3. Running Migrations
To run all migrations at once, execute the following command:
1 2 3 4 5 | php artisan migrate Forcing Migrations To Run In Production |
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:
1 2 | php artisan migrate:rollback |
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.
1 2 3 4 5 | php artisan migrate:refresh // Refresh the database and run all database seeds... php artisan migrate:refresh --seed |
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:
1 2 3 4 | php artisan migrate:fresh php artisan migrate:fresh --seed |
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:
1 2 3 4 | Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); }); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 | if (Schema::hasTable('users')) { // } if (Schema::hasColumn('users', 'email')) { // } |
** 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:
1 2 3 4 | Schema::connection('foo')->create('users', function (Blueprint $table) { $table->bigIncrements('id'); }); |
You can use the following commands on the schema builder to determine table options:
Command | description |
---|---|
$ 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:
1 2 3 4 5 6 | Schema::drop('users'); Schema::dropIfExists('users'); |
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.