How to Drop Table Created and Recreate the Table with Migration Script in Laravel using PHP Artisan Command

Posted on

As shown in the title of this article, it is an article written to show how to drop table created and recreate the table utilizing migration script in a Laravel web-based application. It is done by executing the command available and it is provided by default inside the root folder which is generated upon creating a Laravel web-based application project. The command is executed by using a PHP Artisan command. So, below is the command pattern for doing that :

php artisan migrate:fresh

Below is the command execution in a real situation :

root@hostname:/var/www/html/laravel# php artisan migrate:fresh
Dropped all tables successfully.
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
Migrating: 2017_11_02_150552_create_tickets_table
Migrated: 2017_11_02_150552_create_tickets_table
Migrating: 2017_11_02_151921_create_categories_table
Migrated: 2017_11_02_151921_create_categories_table
Migrating: 2017_11_02_154132_create_comments_table
Migrated: 2017_11_02_154132_create_comments_table
Migrating: 2017_11_03_060734_create_services_table
Migrated: 2017_11_03_060734_create_services_table
Migrating: 2017_11_03_095254_create_units_table
Migrated: 2017_11_03_095254_create_units_table
Migrating: 2017_11_05_120717_role
Migrated: 2017_11_05_120717_role
Migrating: 2017_11_05_120721_user_role
Migrated: 2017_11_05_120721_user_role
Migrating: 2017_11_05_151111_test
Migrated: 2017_11_05_151111_test
root@hostname:/var/www/html/laravel#

The above command is executing all of the available migration script file exist in folder ‘/database/migrations’. Below are the contents of the folder :

user@hostname:/var/www/html/laravel/database/migrations$ pwd
/var/www/html/laravel-support-ticket/database/migrations
user@hostname:/var/www/html/laravel/database/migrations$ ls -al
total 52
drwxrwxr-x 3 www-data user 4096 Nov 6  00:03 .
drwxrwxr-x 5 www-data user 4096 Aug 30 16:55 ..
-rw-rw-r-- 1 www-data user  990 Nov 5 19:05 2014_10_12_000000_create_users_table.php
-rw-rw-r-- 1 www-data user  683 Aug 30 16:55 2014_10_12_100000_create_password_resets_table.php
-rw-rw-r-- 1 www-data user  592 Nov 2 22:05 2017_11_02_150552_create_tickets_table.php
-rw-rw-r-- 1 www-data user  601 Nov 2 22:19 2017_11_02_151921_create_categories_table.php
-rw-r--r-- 1 www-data user  595 Nov 2 22:41 2017_11_02_154132_create_comments_table.php
-rw-rw-r-- 1 www-data user  631 Nov 3 14:36 2017_11_03_060734_create_services_table.php
-rw-rw-r-- 1 www-data user  622 Nov 3 16:53 2017_11_03_095254_create_units_table.php
-rw-rw-r-- 1 www-data user  557 Nov 6 00:03 2017_11_05_120717_role.php
-rw-rw-r-- 1 www-data user  579 Nov 6 00:03 2017_11_05_120721_user_role.php
-rw-rw-r-- 1 www-data user  411 Nov 5 22:11 2017_11_05_151111_test.php
-rw-rw-r-- 1 www-data user  601 Nov 2 22:06 2017_11_02_150646_create_categories_table.php
-rw-rw-r-- 1 www-data user  601 Nov 2 22:12 2017_11_02_151257_create_categories_table.php
user@hostname:/var/www/html/laravel/database/migrations$

It is also specified in the table exist in the laravel web-based application project.

mysql> select * from migrations;
+----+------------------------------------------------+-------+
| id | migration                                      | batch |
+----+------------------------------------------------+-------+
| 1  | 2014_10_12_000000_create_users_table           |   1   |
| 2  | 2014_10_12_100000_create_password_resets_table |   1   |
| 3  | 2017_11_02_150552_create_tickets_table         |   1   |
| 4  | 2017_11_02_151921_create_categories_table      |   1   |
| 5  | 2017_11_02_154132_create_comments_table        |   1   |
| 6  | 2017_11_03_060734_create_services_table        |   1   |
| 7  | 2017_11_03_095254_create_units_table           |   1   |
| 8  | 2017_11_05_120717_role                         |   1   |
| 9  | 2017_11_05_120721_user_role                    |   1   |
| 10 | 2017_11_05_151111_test                         |   1   |
+----+------------------------------------------------+-------+
10 rows in set (0,01 sec)
mysql>

The status of the migration script file is stored in the field named ‘batch’. Every migration process executed the batch value will be updated and added. At first, the table is empty. After the first migration script file execution, the table will be filled with the name of the script in the migration field and the number or the time its migration script is being executed in the batch field.

If it is being reset by executing the command ‘php artisan migrate:reset’, it will then delete all of the entry script in the table migrations. The other one, php artisan migrate:refresh’ will reset or delete all the entry script in the table migrations and runs all the migration script again. So, in the end the batch status will be reset into 1.

One thought on “How to Drop Table Created and Recreate the Table with Migration Script in Laravel using PHP Artisan Command

Leave a Reply