5

I want to rename a controller in Laravel, its Model and its migrations.

I can do it manually, but the problem is that I'll spend a significant amount of time (especially renaming the migrations files'name, the content inside the migration files, and the table 'migrations' in which Laravel keeps track of the migrations).

I can do it now as I have few migrations. So far, the app is small (I just created it to get started with Laravel); I have only a dashboard, a user controller, and an articles controller(that allows users to create and to edit their articles, and allows guests to only see the articles). However, what If that happens again and I have, for example, 30 migrations? I may accidentally mess up everything and cause bugs.

Can I rename a controller automatically? If yes, how I do that? If not, is there something I have to pay special attention when renaming the controller manually? So it won't cause problems after renaming it.

Note: I'm a newbie in Laravel and in StackOverflow, so I'm sorry if my question seems a fool; just don't be rude with me.

  • I doubt if there's a way to automate all this process. However you can use `PHPStorm` that can safely rename + refactor class names but not the migration files. You've to do that manually as per my information. – Zeshan Aug 02 '19 at 18:40
  • Thank you Zeshan Khattak. Indeed, I had to rename the controller manually. It was chaos; I don't recommend anyone to do that; it is better to `rollback` the migrations, delete the controller, and create a new controller than renaming everything. – Student of Science Aug 02 '19 at 19:10
  • I think your answer is here : https://stackoverflow.com/a/13415865/11038906 – mohammad eshaghi Aug 02 '19 at 19:50
  • 1
    Without knowing your app specifics, this is actually a pretty straight-forward process. The Controller and Model can just be renamed, and their usages updated to the new name. The Migration is a little tougher - you should leave your old migrations in place, as-is. Create new migrations to update your table and column names. – Aken Roberts Aug 02 '19 at 20:31
  • migrations are used for database changes. Controllers/Models are php files and can be refactored separately. Im not sure how these 2 are related with such little info about your app. – Flame Aug 02 '19 at 21:04
  • Why would you want to rename the migration files? The renaming itself (of the table, red) is a new migration, I'd argue – Stratadox Aug 02 '19 at 23:15
  • @Stratadox, I didn't think about this solution. Indeed, it is a better idea to create new migrations to rename the tables than to rename the migrations themselves. I already renamed the Controller and all its related stuff, but next time I'll follow your advice – Student of Science Aug 03 '19 at 12:56

1 Answers1

4

The short answer: Don't rename your migrations. Instead, add a table rename migration.

Long answer:

Migrations are a chronological list of the changes made to your database. They are named migrations because they tend to cause a mass movement of data: the data migrates from one structure to another. Renaming a table is one of such migrations.

One of the benefits of having migrations is the ability to roll back the database changes. Assuming your source code is under version control (git, svn, etc) and a release appears faulty after launch, you can simply run a "down migration" and restore the previous (working) version of the application while you work on a fix.

In order for the above scheme to work, it's important to keep your migration files intact. Ideally, you don't ever change a migration: instead you'd add a new migration that incorporates whatever you wanted to change.

To conclude with an example, if you initially named your model Foo, you'd have a migration that comes down to

create_foo migration:
up: create table foo
down: delete table foo

Some time later you figure out you've accidentally named it Foo instead of Bar and rename it. You'd add something that comes down to:

rename_foo_to_bar migration:
up: rename table foo to bar
down: rename table bar to foo

Yet later it turns out Bar should obviously have been Baz:

rename_bar_to_baz migration:
up: rename table bar to baz
down: rename table baz to bar

...but it turns out the release has a bug, so you revert your code and run the down migration

After fixing the bug, you might need to apply a bug-fixing migration:

fix_some_bug migration
up: rename column or something
down: restore the bug

When releasing the bugfix, the database is still at rename_foo_to_bar, so during deployment, both rename_bar_to_baz and fix_some_bug are applied.

By using this system, you can keep your database in sync with the current version of the code you're running.

Don't use down-migrations lightly, though, there's often a risk of losing data.

Stratadox
  • 1,291
  • 8
  • 21
  • Thanks! That is a good recommendation! I should have done that and I'll keep that in mind. Still, I won't mark it as the accepted answer because I'm looking for an automatic way to rename the controller and everything else related to it. – Student of Science Aug 06 '19 at 13:26
  • I don't believe Laravel has an automatic command for this, although it should be possible to create one yourself. It might not be necessary to do so, though: many editors have a feature to rename a class **and all it's usages**. To rename a controller, that would leave you only with updating the routes. – Stratadox Aug 06 '19 at 15:25
  • Notice that controllers don't talk to the database directly; it shouldn't be necessary to use a migration or to change the database at all if you're renaming a controller. – Stratadox Aug 06 '19 at 15:27
  • Stratadox do you know how to rename a class and all it's usages in vscode? I tried `refactoring`, but it actually does not run. – Student of Science Aug 23 '19 at 21:42
  • I accepted your answer. Now, after some weeks using Laravel, I realized my question was not very smart and I should never think in renaming migrations. Still, If someday someone comes with a way to rename the Models, Routes, Controller, etc all together, then I'll accept that answer – Student of Science Aug 23 '19 at 21:44
  • 1
    I'm not very familiar with the visual studio editor, especially for php. I don't think it was a dumb question by the way; I'm sure you're not the only one struggling with these concepts - nobody is born with all the programming knowledge. – Stratadox Aug 25 '19 at 21:01