Laravel #5 - How to Use Rename table with schema in laravel

in #utopian-io7 years ago (edited)

What Will I Learn?


rename from php myadmin maybe everyone can but not everyone understand how to change name using schema laravel. You can rename columns and tables without having to open phpmyadmin to speed up your work. Here's what you'll learn :

  • You will learn rename coulumn of table
  • You will learn rename table

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


Schema is the SQL server security layer. In laravel called Schema builder. Laravel Schema provides a database agnostic way to manipulate tables. This works well with all databases supported by Laravel.Schema Builder provides details such as field values, required fields, and how objects relate to display lookup and master-detail relationships. You can view details for standard and custom objects in Schema Builder.Schema Builder commonly used to create, delete, or other related tables in the database. This scheme will work after migration is run.

Rename is a command to replace a file or folder but inside a laravel scheme. rename is a method used to rename a table or rename a column from a table. Rename is usually used to rename a wrong table or column in the first creation process. this error can occur when tables or columns are created very much. so this method will be very useful to fix the error.

Tutorial Content : Practice Using Rename in Laravel


in this tutorial we will rename the columns and tables. before doing rename we will create 2 new tables. so we can try rename all tables in one schema.

Step 1 : create table rename1 and rename2

This is the table that we will use in this tutorial. for step 1 we will create a table named rename1 and rename2. in making this table we will try to make 2 pieces of tables in one scheme who we did not have time to try in the previous tutorial. so use the command below to create a migration file.

php artisan make:migration create_table_rename1_and_rename2

after that open the migration file and add this code:

    public function up()
    {
      Schema::create('rename1', function (Blueprint $table) {
          $table->integer('id_post');
          $table->increments('id_p1');
          $table->string('name',60);
          $table->text('titlepost');
      });

      Schema::create('rename2', function (Blueprint $table) {
          $table->increments('id_p2');
          $table->string('tutorialname',60);
          $table->text('suggestion');
      });

    }

Here's a little explanation :

  • Schema::create('rename1', function (Blueprint $table) { this is the scheme used to create a table rename1
  • Schema::create('rename2', function (Blueprint $table) { this is the scheme used to create a table rename2

for other code you can look the explanation in my previous post. and you need to remember in creating a table you should use Schema :: create and do not use Schema :: table because this is used for table modification. 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 table from database
table.png

and now you understand. we can create 2 or more tables in the migration file with 2 different schemes. now we will go step 2 to edit the column name because we already have 2 new tables.

Step 2 : Using Rename to change name of coulumn from table rename1 and rename2

In this step. we will rename the table columns from the rename1 and rename2 tables. First, we will rename the column from the rename1 table. Secondly, we will rename the table columns rename1 and rename2 at once using 1 migration file. here we will prove if we can create 2 tables using 1 migration file should we also can rename column from 2 table using 1 file migration.

before rename column from table rename1 and rename2. we will look the structure of the table.
image

now we will start from table rename1. we will rename the column post_id to SBD. then use the following command to create a migration file named rename_column1.

php artisan make:migration rename_coulumn1

after that open the migration file and add this code:

    public function up()
    {
      Schema::table('rename1', function (Blueprint $table) {
         $table->renameColumn('id_post', 'SBD');
      });
    }

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

php artisan migrate

This is a show of structure table from rename1
rename_table1.png

as you can see, we can rename the columns of 1 table in 1 migration file. but how about 2 tables in one file migration ?. can we change the column name of 2 tables at once in 1 file migration ?. well we will try it now. create a new migration file named rename_column2 using the script below.

php artisan make:migration rename_coulumn2

after that open the migration file and add this code:

    public function up()
    {
      Schema::table('rename1', function (Blueprint $table) {
         $table->renameColumn('id_p1', 'steemit_id');
         $table->renameColumn('name', 'blog');
      });
      Schema::table('rename2', function (Blueprint $table) {
         $table->renameColumn('id_p2', 'utopian_id');
         $table->renameColumn('tutorialname', 'contribution');
      });
    }

Here's a little explanation :

  • Schema::table('rename1', function (Blueprint $table) { this is the scheme used to modification a table rename1
  • Schema::table('rename2', function (Blueprint $table) { this is the scheme used to modification a table rename2
  • $table->renameColumn('id_p1', 'steemit_id'); this is the method used to renamecolumn from id_p1 to steemit_id. and other code is same with this .other code only object who different.

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

php artisan migrate

This is a show of structure table from rename1 and rename2
rename_table2.png

now proven. we can also rename the columns of the 2 tables in 1 migration file. how to create a table. You should only use 2 schemes in 1 migration file. in this case we use schema :: table 'rename1' for the rename1 table and schema :: table 'rename2' for the rename2 table.

Step 3 : Using Rename to change name of table from table rename1 and rename2

In this final step I will teach how to rename the tables rename1,rename2 and tutorial. First we start from rename1. we will replace the table name from rename1 to mypost.create a new migration file named rename_table1 using the script below.

php artisan make:migration rename_table1

after that open the migration file and add this code:

    public function up()
    {
        Schema::rename('rename1', 'mypost');
    }

Here's a little explanation :

  • Schema::rename('rename1', 'mypost'); this is the scheme used to rename a table from rename1 to mypost

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

php artisan migrate

This is a show of table from my database
table_rename1.png

Because we have successfully renamed a table. now we will try to replace 3 table names at once. why we use 3 tables? whereas in the previous step we only use 2 tables. because i want to prove to you. we can not only rename 2 tables in one migration file but more than that. you can replace all table names in your database in 1 migration file.well we will try it now. create a new migration file named rename_table2 using the script below.

php artisan make:migration rename_table2

after that open the migration file and add this code:

    public function up()
    {
        Schema::rename('rename2', 'utopian');
        Schema::rename('tutorial', 'laravel');
        Schema::rename('rename1', 'steemit');
    }

Here's a little explanation :

  • Schema::rename('rename2', 'utopian'); this is the scheme used to rename a table from rename2 to utopian
  • Schema::rename('rename1', 'mypost'); this is the scheme used to rename a table from tutorial to laravel
  • Schema::rename('mypost', 'steemit'); this is the scheme used to rename a table from mypost to steemit.

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

php artisan migrate

This is a show of table from my database now
rename_database_table2.png

Now you understand what it takes to replace more than one table name in 1 migration file, just a schema. and if you take a good look at the script rename_table2. I make it not as sequential as in the database and this is also to prove that you can type the code from anywhere the first comes into your mind but you should make it sequentially as in the database so you are not confused when there is an error.

Conclusion


Rename can be used at once to replace all column or table names using 1 migration file and you only need to add one schema for one table. And the following is the command used to rename the table and the column name of the table

  • Schema::rename('before', 'after') is the Scheme used to rename a table.
  • $table->renameColumn('before', 'after'); is the method used to change the column names of a table

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 @kizilelma, 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!

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

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.030
BTC 59347.70
ETH 2534.40
USDT 1.00
SBD 2.47