How to Use Feature Dropping and Rollback with Migration in Laravel

in #utopian-io7 years ago (edited)

What Will I Learn?


Rollback and dropping have the same functionality but they have a clear distinction. Here's what you'll learn so you understand how to differentiate the two :

  • You will learn to use dropping in laravel
  • You will learn to use rollback in laravel

Requirements


to follow this tutorial. you only need to provide some tools. so this is list of the requirements for the user:

  • Xampp 5.6.3
  • Framework Laravel 5.4
  • Composer
  • Atom text editor

Difficulty


  • Basic

Preliminary


Dropping is a feature used to delete in a database. dropping is used to delete a table in the database or delete a column from the table. this feature can also be used to delete multiple columns in one command but can not be used to delete multiple tables.

Rollback is a feature used to return or cancel. Rollback is usually used to undo the last action of migration. Rollback can be interpreted as a restore that is used when there is an error that occurs in the migration.Rollback process is used to return to starting point before migration. Rollback can be done in several ways and one of them we can determine how many step to back who we want to rollback. if we have been doing too much migration. Rollback can also be interpreted back to the past and once you return to the past using rollback you will not be able to return to the future. so this is just a feature like undo but does not have redo. so use this feature when there is a very fatal error.

In brief, both of these features can be used to remove tables or columns from the table. but you need to remember Rollback works like undo buttons while Dropping works like a delete button. same but diffrent.

Practice using Dropping and Rollback


Here is a dropping and rollback step. we will do it in the tutorial table because this is the table we created in the previous tutorial.

step 1: Using Drop to delete column of table

before deleting the table column we will see the column structure of the tutorial table and we will try to delete some columns

here is a view of the structure of the table tutorial:
image

there are 9 columns visible in the table. and we will try to delete the category of column and all type of text from columns of table.The first, you must create migration.we making file migration with names is drop_column_of_table and you can use the command below :

php artisan make:migration drop_column_of_table

This is snow on command prompt:

image

why I always display the results of running code in the command prompt because to help you know the correct running results. now open the migration file and add this code:

  public function up()
    {
      Schema::table('tutorial', function (Blueprint $table) {
          $table->dropColumn('category');
          $table->dropColumn(['title', 'author', 'contentpage']);
       });
   }

Here's a little explanation :

  • $table->dropColumn('category'); is the method used to delete a single column that is category..
  • $table->dropColumn(['title', 'author', 'contentpage']); is the method used to delete multiple columns or more than one column and the columns that are deleted are title , author and contentpage..

after that, use this script to execute migration files that have been edited

php artisan migrate

Here is an example that runs in the command prompt:
image

This is a show of the tutorial table we have dropping :
image

step 2: Using Drop to delete table from database.

If previously we have managed to delete some columns from the tutorial table. now we will try to remove the tutorial table from the database and before deleting the table we will see the existing table in the database

here is a view of the table from my database and maybe different with you because i don't know how much table have you create before:

image

there are 2 tables there. and we will delete 1 table ie tutorial. before deleting it we make first file migration with name drop_table_tutorial.and you can use the command below :

php artisan make:migration drop_table_tutorial

This is snow on command prompt:
image

after that you can add this code on file migration above :

  public function up()
    {
      Schema::drop('tutorial');
    }

Here's a little explanation :

  • Schema::drop('tutorial'); is the method used to delete the tutorial table

why this code is different from step 1. because we use it directly to delete the table. so we no need to use $ table. just use the schema :: drop. after that you can use this code for execute file migration :

php artisan migrate

and here is an example that runs in the command prompt:
image

and this is evidence , table tutorial have successful we delete from database.
image

step 3: Using Rollback with migration for comingback all.

If you already know how to dropping table. now I will teach you about rollback in the last step and I will prove this can not only be used to delete the table in the way . I will teach you use rollback with different way.

in the file migration. there is method function up () in execute using php artisan migrate . so, if you want execute method function down(). You can use php artisan migrate:rollback . okay let's begin.

you can open file migration drop_table_tutorial and nothing code in method function down() and then will do rollback and see result. before rollback we see status migration with this script .

php artisan migrate:status

here is display of status before rollback. if we have rollback . column of Ran will change be N
rollback1.PNG

now we use rollback and we look database. what is tutorial table back because. if migration before for drop table .rollback can back table to database because we comeback to before drop table.now use this script for rollback:

php artisan migrate:rollback

and this is status now . after running rollback :
rollback2.PNG

and this is result from database
image

now we try edit method function down() from file migration drop_column_of_table because next rollback . that's file who be execute. you can see in the last status. rows number 2 still Y after we rollback . that will change be N. now you can add this script to method function down() in that's file migration

  public function down()
    {
      Schema::create('tutorial', function (Blueprint $table) {
          $table->integer('post_id');
          $table->increments('id');
          });
    }

now we will use rollback. you can use this script

php artisan migrate:rollback

and this is status after execute:
image

and this is database after execute:
rollback3.PNG

and rollback proven is used to execute method function down() so if you want delete table with rollback you must use this code Schema::drop('your table'); or this code Schema::dropIfExists('your table'); . now we will look structure of table tutorial to convince you. we create table tutorial with column post_id and id if equal means true

here is a view of the structure of the table tutorial after rollback:
image

rollback can be used in several ways. and if you want to try. you can use one of these codes:

  • migrate:rollback --step=1 is the command used to rollback according to the number you choose. so you can see in status. and here we use the 1st order to rollback
  • migrate:reset is the command used to rollback everything from start to finish
  • migrate:refresh is the command used to rollback everything from start to finish and then rerun migration from start to finish

conclusion:


  • Schema::drop is used to delete the table from the database

  • $table-> dropColumn is used to delete the column of table from the database

  • $migrate:rollback used to execute method function down()

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @deathwing, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

@iwaydi, Upvote is the only thing I can support you.

Thanks you. You always support me @steemitstats

please vote for each other

Hey @iwaydi I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Thank you very useful for all

Coin Marketplace

STEEM 0.15
TRX 0.15
JST 0.028
BTC 54034.48
ETH 2262.26
USDT 1.00
SBD 2.31