0

I'm still new as a developer and I'm trying to change user table, were I add a new column "user_type" from the previous table.

My question is: Can I do that or should I do the php artisan make:migration add_user_type_users ?

So if I can change the main table without using add_user_type_users, how should I migrate it into laravel forge before I can see the changes. Because, when I commit the change to the forge it won't add my new column user_type.enter image description here

NEW INFO

How do I commit the changes into the live database? I'm using Laravel Forge and DigitalOcean

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sajid Latif
  • 119
  • 2
  • 16
  • 2
    Don't modify a migration that's already run, unless you plan to refresh the database. Instead, you'll have to create another migration file to modify the existing table. – aynber Apr 12 '19 at 14:11
  • I want to refresh the database as well – Sajid Latif Apr 12 '19 at 14:11
  • How do I create another migration file to modfy the existing table? – Sajid Latif Apr 12 '19 at 14:12
  • If you're going to refresh the database, which will delete all of the tables then recreate it, then go ahead and modify the old migration file. Just know that changes won't take effect until you do the refresh. – aynber Apr 12 '19 at 14:12
  • Then you would use the command you said, `php artisan make:migration add_user_type_to_users_table`, which will create a file with `Schema::table` instead of `Schema::create`. – aynber Apr 12 '19 at 14:15
  • Follow [this steps](https://stackoverflow.com/a/55629351/6556397) – Rahul Apr 12 '19 at 14:16
  • Possible duplicate of [Add new columns to existing table in a migration in Laravel](https://stackoverflow.com/questions/55629242/add-new-columns-to-existing-table-in-a-migration-in-laravel) – aynber Apr 12 '19 at 14:19
  • I have made a php artisan migrate:reset and it works fine. But how to deploy it into the forge/digital ocean? Normally I would make a commit, but in this example there is nothing to commit – Sajid Latif Apr 14 '19 at 05:02

1 Answers1

1

(edit: answer original question)

if you need to add news columns to existent table, you must:

  1. create a new migration file: php artisan make:migration <fileName>
  2. use Schema::table() instead Schema::create() on the migration file
  3. add columns like a common migration
  4. run the migration: php artisan migrate

example:

//change create for table
//the first param (in this case 'users') must be the table name that you want to add columns
Schema::table('users', function (Blueprint $table) {
        //news colums here
        $table->string('new_col');
    });

EDIT: answer edited quiestion:

Can I do that or should I do the php artisan make:migration add_user_type_users ?

-

IS POSIBLE BUT ISN´T THE CORRECT WAY:

Yes you can edit your old migration file BUT if you do that, your nexts php artisan migrate will not change the already migrate files, and you here you MUST delete your table and his data in this case, and delete from migrations table for run your edited migration file. so this isn´t the answer for production env. If you do dat you take a lot of problems that you would avoid. maybe you can do this way on a dev. env. because you can delete and create tables everytime you want without lose real data.

-

CORRECT WAY:

you MUST create the new migration file (like the original answer), and you will not have problems with lose data, (unless you want this) and the migration will run on production without remove anyyhing.

otosankf
  • 36
  • 3
  • I have made a php artisan migrate:reset and it works fine. But how to deploy it into the forge/digital ocean? Normally I would make a commit, but in this example there is nothing to commit – Sajid Latif Apr 14 '19 at 05:02
  • Who said migrate reset? – otosankf Apr 14 '19 at 05:04
  • No one, I'm just assuming that it would rollback all the migration and start over. Is that wrong? – Sajid Latif Apr 14 '19 at 05:04
  • Read de answer: 1. Creatw a new migration (1 new and keep the old migrations whitout change) – otosankf Apr 14 '19 at 05:07
  • If you follow the steps. You havent to lose any data – otosankf Apr 14 '19 at 05:10
  • I misunderstood the laravel concept. I thought that It was possible to reset the production if I needed to add a column. I have now changed the column by adding it the way Laravel wants me to. Thanks for your help @otosankf – Sajid Latif Apr 16 '19 at 05:07